Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 16776 | Accepted: 8077 |
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
跳到的那个位置之前的所有字符与当前失配字母前的相同数量个字母是相匹配的
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=1e6+5; int n,f[N]; char p[N]; int main(){ int cas=0; while(scanf("%d",&n)!=EOF&&n){ scanf("%s",p); f[0]=f[1]=0; for(int i=1;i<n;i++){ int j=f[i]; while(j&&p[j]!=p[i]) j=f[j]; f[i+1]=p[j]==p[i]?j+1:0; } printf("Test case #%d\n",++cas); for(int i=2;i<=n;i++) if(f[i]>0&&i%(i-f[i])==0) printf("%d %d\n",i,i/(i-f[i])); printf("\n"); } }
原文:http://www.cnblogs.com/candy99/p/5977986.html