POJ
来源:互联网 发布:英雄传奇挂机软件 编辑:程序博客网 时间:2024/06/03 18:22
题意
给一个字符串(长度不超过1e6),要把它看成 某个子串重复 x 次形成,求 x 的最大值。
思路
kmp算法中next数组的应用。
链接
https://cn.vjudge.net/contest/177933#problem/I
代码
#include<cstdio>#include<iostream>#include<cstring>using namespace std;const int maxn = 1e6 + 10;char str[maxn];int next[maxn];int n;void get_next(){ int j = 0, k = -1; next[0] = -1; while(j < n) { if(k == -1 || str[j] == str[k]) next[++j] = ++k; else k = next[k]; }}int main(){ //freopen("in.txt", "r", stdin); while(scanf(" %s", str)) { if(str[0] == '.') break; n = strlen(str); get_next(); int res = 1; if(n % (n - next[n]) == 0) res = n / (n - next[n]); cout << res << endl; } return 0;}