标签:ace name poj lse not uva 循环 == gate
一年前做的.
求一个字符串由多少个循环节构成.
KMP.
结论:若\(n \% (n-next[n]) \not= 0\)则不是由循环节构成的.
否则,答案为\(n/(n-next[n])\).
code
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#define MogeKo qwq
using namespace std;
#include<string>
const int maxn = 1e6+10;
int n,f[maxn];
char s[maxn];
int main() {
while(1) {
scanf("%s",s);
if(s[0] == ‘.‘)break;
n = strlen(s);
f[0] = f[1] = 0;
for(int i = 1; i < n; i++) {
int j = f[i];
while(j && s[i]!=s[j]) j = f[j];
if(s[i] == s[j]) f[i+1] = j+1;
else f[i+1] = 0;
}
if(n%(n-f[n])!=0) printf("1\n");
else printf("%d\n",n/(n-f[n]));
}
return 0;
}
UVA10298 POJ2046 Power Strings
标签:ace name poj lse not uva 循环 == gate
原文地址:https://www.cnblogs.com/mogeko/p/13217373.html