hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节)
2013-06-17 14:30
417 查看
Problem - 3374
KMP求循环节。
/article/5031263.html
循环节推导的证明相当的好,这题是很裸的套算法的题。
代码如下:
View Code
——written by Lyon
KMP求循环节。
/article/5031263.html
循环节推导的证明相当的好,这题是很裸的套算法的题。
代码如下:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 1111111; char buf ; int next ; void getNext(char *str) { char *si = str; int *ni = next; int j = *next = -1; while (*si) { while (j > -1 && *si != *(str + j)) j = *(next + j); si++, ni++, j++; // if (*si == *(str + j)) *ni = *(next + j); // else *ni = j; *ni = j; } } int minMaxExp(char *s, bool mini) { int i = 0, j = 1, k = 0, t; int len = strlen(s); while (i < len && j < len && k < len) { // cout << i << ' ' << j << ' ' << k << endl; t = s[(i + k) % len] - s[(j + k) % len]; if (!t) k++; else { if (mini ^ (t > 0)) j += k + 1; else i += k + 1; if (i == j) j++; k = 0; } } return min(i, j); } int main() { while (cin >> buf) { getNext(buf); // for (int i = 0, sz = strlen(buf); i < sz; i++) cout << next[i] + 1 << ' '; cout << endl; int cycle = strlen(buf); // cout << "got next" << endl; cycle /= (cycle - next[cycle - 1] - 1); cout << minMaxExp(buf, true) + 1 << ' ' << cycle << ' ' << minMaxExp(buf, false) + 1 << ' ' << cycle << endl; } return 0; }
View Code
——written by Lyon
相关文章推荐
- hdu 3374 String Problem(字符串最小最大表示法+kmp)
- hdu 3374 String Problem(KMP+字符串最小最大表示)
- HDU 3374 String Problem (KMP+最大最小表示)
- HDU 3374 String Problem (KMP+最大最小表示)
- HDU 3374 String Problem (KMP+最大最小表示)
- hdu 3374 String Problem (kmp+最大最小表示法)
- HDU 3374 String Problem (KMP+最大最小表示)
- hdu 3374 String Problem (kmp+最大最小表示法)
- HDU 3374 String Problem (KMP+最大最小表示)
- HDU 3374 String Problem (KMP+最大最小表示)
- 【字符串最小(大)表示法+KMP求循环节】hdoj 3374 String Problem
- HDU 3374 String Problem(字符串最小表示+KMP )
- HDU 3374 String Problem(最大最小表示法模板+KMP+next数组的运用)
- HDU 3374 String Problem (KMP+最大最小表示法)
- HDU 3374 String Problem (KMP+最小最大表示)
- HDU 3374 String Problem(KMP求周期+求串最大最小表示法)
- String Problem - HDU 3374 (kmp+最大最小表示)
- HDU 3374 String Problem (KMP+最大最小表示)
- hdu 3374 String Problem (最小表示法+KMP)
- 【KMP】 HDU 3374 String Problem 最小表示法