天天看點

Period II(FZU-1901)Problem DescriptionInputOutputSample InputSample OutputSource Program

Problem Description

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

4

ooo

acmacmacmacmacma

fzufzufzuf

stostootssto

Sample Output

Case #1: 3

1 2 3

Case #2: 6

3 6 9 12 15 16

Case #3: 4

3 6 9 10

Case #4: 2

9 12

題意:t 組資料,每組給出一個字元串,對于字元串的所有字首,若存在循環節,輸出這個字元串的符合條件的個數與這個字元串的長度

思路:思路與 Seek the Name, Seek the Fame(POJ-2752)類似,但輸出的不是循環節的長度,而是字首子串的長度,此外需要統計符合條件的子串個數,可利用隊列來統計輸出

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-9
#define INF 0x3f3f3f3f
#define N 1000001
#define LL long long
const int MOD=20091226;
const int dx[]= {-1,1,0,0};
const int dy[]= {0,0,-1,1};
using namespace std;

int Next[N];
char p[N];
int n;
void getNext(){

    Next[0]=-1;

    int len=strlen(p);
    int j=0;
    int k=-1;

    while(j<len){
        if(k==-1||p[j]==p[k]) {
            k++;
            j++;
            Next[j]=k;
        }else{
            k=Next[k];
        }
    }
}
int main(){
    int t;
    scanf("%d",&t);

    int Case=1;
    while(t--){
        scanf("%s",p);
        int len=strlen(p);
        getNext();

        int i=len;
        queue<int> Q;
        while(i){
            i=Next[i];
            Q.push(i);
        }

        printf("Case #%d: %d\n",Case++,Q.size());
        printf("%d",len-Q.front());
        Q.pop();
        while(!Q.empty()){
            printf(" %d",len-Q.front());
            Q.pop();
        }
        printf("\n");
    }
    return 0;
}