POJ 2406 Power Strings 1961的简化版,kmp的next数组的应用
题目: http://poj.org/problem?id=2406
跟1961差不多,题解就不写了,一开始理解错题了,导致WA一次。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 int n, next[1000010]; 5 char s[1000010]; 6 void kmp_init() 7 { 8 int j = -1; 9 next[0] = -1; 10 for(int i = 1; i < n; i++) 11 { 12 while(j >= 0 && s[j+1] != s[i]) 13 j = next[j]; 14 if(s[j+1] == s[i]) 15 j++; 16 next[i] = j; 17 } 18 } 19 int main() 20 { 21 while(scanf("%s", s) != EOF && strcmp(s, ".")) 22 { 23 int ans = 1; 24 n = strlen(s); 25 kmp_init(); 26 if(n % (n-1 - next[n-1]) == 0) 27 ans = n / (n-1 - next[n-1]); 28 printf("%d\n", ans); 29 } 30 return 0; 31 }