标签:des style blog http color os io ar strong
Time Limit: 3000MS | Memory Limit: 65536K | |
Description
Input
Output
Sample Input
abcd aaaa ababab .
Sample Output
1 4 3
Hint
Source
第一道kmp题。。题目意思是求一个字符串的循环节,求出循环节长度之后检验即可
1 #include<set> 2 #include<queue> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 const int N = 1000010; 10 #define For(i,n) for(int i=1;i<=n;i++) 11 #define Rep(i,l,r) for(int i=l;i<=r;i++) 12 char s[N]; 13 int next[N],n; 14 15 void BuildNext(char s[]){ 16 next[0]=next[1]=0; 17 For(i,n-1){ 18 int j=next[i]; 19 while(j&&s[i]!=s[j]) j=next[j]; 20 if(s[i]==s[j]) next[i+1]=j+1; 21 else next[i+1]=0; 22 } 23 } 24 25 int main(){ 26 while(scanf("%s",&s),s[0]!=‘.‘){ 27 n=strlen(s);BuildNext(s); 28 int rpt = n-next[n]; 29 int i = n; 30 while(i&&i-next[i]==rpt) i=next[i]; 31 if(i) printf("1\n"); 32 else printf("%d\n",n/rpt); 33 } 34 return 0; 35 }
标签:des style blog http color os io ar strong
原文地址:http://www.cnblogs.com/kjerome/p/3956794.html