poj 2406 Power Strings

題目鏈接

求一個字符串的循環節爲多大,直接用kmp的next的數組求法寫

#include <stdio.h>
#include <string.h>
using namespace std;
char s[1123456];
int next[1123456];
int main()
{
    while(~scanf("%s", s)){
        if(s[0] == '.'){
            break;
        }
        int i = 0;
        int j = -1;
        next[0] =-1;
        int n = strlen(s);
        while(i < n){
            if(j == -1||s[i] == s[j]){
                i++;
                j++;
                next[i] = j;
            }else {
                j = next[j];
            }
        }
        if(n%(n-next[n])==0){
            printf("%d\n", n/(n-next[n]));
        }else{
            printf("1\n");
        }
    }

    return 0;
}


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