本文主要是介绍Power Strings POJ - 2406(kmp求循环节),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Given two strings a and b we define ab to be their concatenation. For example, if a = “abc” and b = “def” then ab = “abcdef”. If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = “” (the empty string) and a^(n+1) = a*(a^n).
Input
Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.
Output
For each s you should print the largest n such that s = a^n for some string a.
Sample Input
abcd
aaaa
ababab
.
Sample Output
1
4
3
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
题意: 求最小循环节
思路: 今年秦皇岛和国庆训练都出现了kmp签到,还是得复习复习
#include <cstdio>
#include <cstring>
#include <algorithm>using namespace std;char s[1000007];
int f[1000007],n;void getFail()
{f[0] = f[1] = 0;for(int i = 2;i <= n;i++){int j = f[i - 1];while(j && s[i] != s[j + 1]){j = f[j];}f[i] = s[j + 1] == s[i] ? j + 1 : 0;}
}int main()
{while(~scanf("%s",s + 1)){if(s[1] == '.')break;n = (int)strlen(s + 1);getFail();if(n % (n - f[n]) == 0){printf("%d\n",n / (n - f[n]));}else printf("%d\n",1);}return 0;
}
这篇关于Power Strings POJ - 2406(kmp求循环节)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!