Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "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
Output
Sample Input
abcd
aaaa
ababab
.
Sample Output
1
4
3
Hint
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; typedef long long ll; int n,lb,la,crt; char a[1000005]; int nextt[1000005]; void kmp() { int k = -1, j = 0; nextt[0] = -1; while (j < la) { if (k == -1 || a[j] == a[k]) { j++; k++; nextt[j] = k; } else k = nextt[k]; } } void solve() { if(la% (la - nextt[la]) == 0) printf("%d ", la / (la - nextt[la])); else puts("1"); } int main() { while (scanf("%s", a)!=EOF) { if (a[0] == '.') break; la = strlen(a); kmp(); solve(); } return 0; }
For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as A K ,that is A concatenated K times, for some string A. Of course, we also want to know the period K.Input
number zero on it.
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
emmmmmmm……没什么好说的,跟上边代码一样一样的……
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; typedef long long ll; int n,la,crt=0; char a[1000005]; int nextt[1000005]; void kmp() { int k = -1, j = 0; nextt[0] = -1; while (j < n) { if (k == -1 || a[j] == a[k]) { j++; k++; nextt[j] = k; } else k = nextt[k]; } } void solve() { printf("Test case #%d ", crt); for(int i=1;i<=n;i++) if(i%(i - nextt[i]) == 0&& i!=(i - nextt[i])) printf("%d %d ",i, i / (i - nextt[i])); puts(""); } int main() { while (~scanf("%d", &n)&&n) { crt++; scanf("%s", a); kmp(); solve(); } return 0; }