KMP is helpless to me, and I am really worried about KMP. It's just an algorithm. I thought it could be done by the stuff around my neck. Now it's okay-it's so big that I have a big head. I want to write a value for next, five words: That's not a problem. When it comes to practical application, I don't know what the next function is and what it can do. It's like seeing two points --
I 've been searching for the same string for a long time, for example, acmacmacmacmacma. Just look at next from the back, for example, the last next [15] = 13, it indicates that the first 13 strings are the same as the last 13 digits, and the total length is 16-13 = 3, which is used as a solution. Next, let's look at next [13] And keep searching for the result, pay attention to the format when outputting the data. I handed in four errors here.
For each prefix with length P of a given string s, if
S [I] = s [I + P] For I in [0 .. size (S)-P-1],
Then the prefix is a "period" of S. We want to all the periodic prefixs.
Input
Input contains multiple cases.
The first line contains an integer t representing the number of cases. Then following T cases.
Each test case contains a string S (1 <= size (s) <= 1000000), represents the title. s consists of lowercase, uppercase letter.
Output
For each test case, first output one line containing "case # X: Y", where X is the case number (starting from 1) and Y is the number of periodic prefixs. then output the lengths of the periodic prefixs in ascending order.
Sample Input
4oooacmacmacmacmacmafzufzufzufstostootssto
Sample output
Case #1: 31 2 3Case #2: 63 6 9 12 15 16Case #3: 43 6 9 10Case #4: 29 12
#include <iostream>#include<cstring>using namespace std;int a[1000100],next[1000100];int m;char s[1000100];void getNext(){ int j; next[0] = 0; next[1] = 0; for(int i = 1;i < m;i++) { j = next[i]; while(j && s[i]!=s[j]) { j = next[j]; } next[i+1] = s[i] == s[j]?j+1:0; }}int main(){ int n,count,where = 1; cin >> n; while(n--) { cin >> s; m = strlen(s); // cout<< m; count = 0; int t = m; getNext(); while(next[m]) { a[count++] = t - next[m]; m = next[m]; } cout << "Case #" << where <<": " << count+1 << endl; where++; for(int i = 0;i < count ;i++) { cout << a[i] << " "; } cout <<t << endl; } return 0;}