【POJ 1961】Period(KMP求循环节)
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 15466 | Accepted: 7417 |
Description
Input
Output
Sample Input
3 aaa 12 aabaabaabaab 0
Sample Output
Test case #1 2 2 3 3 Test case #2 2 2 6 2 9 3 12 4
Source
2406的升级版,串长i循环跑一遍即可,其他参见2406
代码如下:
#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <stack> #include <list> #include <algorithm> #include <map> #include <set> #define LL long long #define Pr pair<int,int> #define fread() freopen("in.in","r",stdin) #define fwrite() freopen("out.out","w",stdout) using namespace std; const int INF = 0x3f3f3f3f; const int msz = 10000; const int mod = 1e9+7; const double eps = 1e-8; char str[2333333]; int Next[2333333]; int n; void GetNext() { int j,i = 0; j = Next[0] = -1; while(i < n) { while(j != -1 && str[i] != str[j]) j = Next[j]; ++i,++j; Next[i] = j; } } int main() { //fread(); //fwrite(); int z = 1; while(~scanf("%d",&n) && n) { scanf("%s",str); GetNext(); printf("Test case #%d\n",z++); for(int i = 1; i <= n; ++i) { int len = i-Next[i]; if(i != len && i%len == 0) printf("%d %d\n",i,i/len); } puts(""); } return 0; }