hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节)
生活随笔
收集整理的這篇文章主要介紹了
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轉載于:https://www.cnblogs.com/LyonLys/p/hdu_3374_Lyon.html
總結
以上是生活随笔為你收集整理的hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle 11g 卸载
- 下一篇: 入门视频采集与处理(学会分析YUV数据)