ACM training II C

Source: Internet
Author: User

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;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.