uva10298 Power Strings

这题就是求一个字符串的最小循环节长度,然后ans = len / k;
if (len % (len - nxt[len]) == 0) ans = len / (len - nxt[len]);
else ans = 1;

const int maxn = 1e6 + 10;
char s[maxn];
int nxt[maxn];
void get_nxt() {
    int i = 0, j = -1;
    nxt[0] = -1;
    int len = strlen(s);
    while(i < len) {
        while(j != -1 && s[i] != s[j]) j = nxt[j];
        nxt[++i] = ++j;
    }
}
int main(int argc, const char * argv[])
{   
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    // clock_t _ = clock();

    while(scanf("%s", s) != EOF) {
        if (s[0] == '.') break;
        get_nxt();
        int len = strlen(s);
        if (len % (len - nxt[len]) == 0) cout << len / (len - nxt[len]) << endl;
        else cout << 1 << endl;
    }

    // printf("\nTime cost: %.2fs\n", 1.0 * (clock() - _) / CLOCKS_PER_SEC);
    return 0;
}

你可能感兴趣的:(KMP)