• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LyonLys
愿意在角落唱沙哑的歌 再大声也都是给你 请用心听 不要说话 Contact me via E-mail: lyon.lys@gmail.com
博客园    首页    新随笔    联系   管理    订阅  订阅

hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节)

Problem - 3374
 
KMP求循环节。
http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html
 
  循环节推导的证明相当的好,这题是很裸的套算法的题。
代码如下:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 const int N = 1111111;
 9 char buf[N];
10 int next[N];
11 
12 void getNext(char *str) {
13     char *si = str;
14     int *ni = next;
15     int j = *next = -1;
16     while (*si) {
17         while (j > -1 && *si != *(str + j)) j = *(next + j);
18         si++, ni++, j++;
19 //        if (*si == *(str + j)) *ni = *(next + j);
20 //        else *ni = j;
21         *ni = j;
22     }
23 }
24 
25 int minMaxExp(char *s, bool mini) {
26     int i = 0, j = 1, k = 0, t;
27     int len = strlen(s);
28     while (i < len && j < len && k < len) {
29 //        cout << i << ' ' << j << ' ' << k << endl;
30         t = s[(i + k) % len] - s[(j + k) % len];
31         if (!t) k++;
32         else {
33             if (mini ^ (t > 0)) j += k + 1;
34             else i += k + 1;
35             if (i == j) j++;
36             k = 0;
37         }
38     }
39     return min(i, j);
40 }
41 
42 int main() {
43     while (cin >> buf) {
44         getNext(buf);
45 //        for (int i = 0, sz = strlen(buf); i < sz; i++) cout << next[i] + 1 << ' '; cout << endl;
46         int cycle = strlen(buf);
47 //        cout << "got next" << endl;
48         cycle /= (cycle - next[cycle - 1] - 1);
49         cout << minMaxExp(buf, true) + 1 << ' ' << cycle << ' ' << minMaxExp(buf, false) + 1 << ' ' << cycle << endl;
50     }
51     return 0;
52 }
View Code

 

 
——written by Lyon
posted @ 2013-06-17 14:30  LyonLys  阅读(151)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3