POJ 1961 Period

求一個字符串在第幾位上出現了循環節,輸出位數 以及循環節的大小

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <algorithm>

using namespace std;

int main()
{
    char s[1000100];
    int t;
    int Case = 1;
    while(~scanf("%d", &t)){
        if(t == 0) break;
        scanf("%s", s);
        int next[t+1];
        int i = 0,j = -1;
        next[0] = -1;
        while(i < t){
            if(j == -1||s[i]==s[j]){
                i++;
                j++;
                next[i] = j;
            }else{
                j=next[j];
            }
        }
        printf("Test case #%d\n", Case);
        for(i = 2;i<=t;i++){
            if(i%(i-next[i])==0&&i/(i-next[i])>1){
                printf("%d %d\n", i, i/(i-next[i]));
            }
        }
        cout <<endl;
        Case++;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章