字符串Hash
对于一个字符串,枚举长度,在字符串中用$hash$进行比较
比如对于区间[l,r],那么这一串字符的$hash$值为$hash[r]-base^{r-l+1}*hash[l-1]$
进行枚举即可
复杂度为$nsum_{i=1}^{n}frac{1}{i}$
由于$int frac{dx}{x}=ln x$
所以$sum_{i=1}^{n}frac{1}{i}leq ln n$
因此复杂度为$O(nln n)$
#include <bits/stdc++.h> #define mod 1000000007 #define base 719 #define ll long long using namespace std; const ll MAXN=1001000; char ch[MAXN]; ll ha[MAXN],z[MAXN]; int main() { z[0]=1; for (ll i=1;i<=MAXN-10;i++) z[i]=(z[i-1]*base)%mod; while (1) { scanf("%s",ch+1); if (ch[1]=='.') break; ll len; len=strlen(ch+1); ha[0]=0; for (ll i=1;i<=len;i++) ha[i]=(ha[i-1]*base%mod+(ll)ch[i]%mod)%mod; ll aim; for (ll length=1;length<=len;length++) { aim=(ha[length]-(ha[0]*z[length]%mod)+mod)%mod;//目标 if (len%length==0) { bool bl=1; for (ll r=length*2;r<=len;r+=length) { ll h; h=(ha[r]-(ha[r-length]*z[length]%mod)+mod)%mod; if (aim!=h) { bl=0; break; } } if (bl) { printf("%lld ",len/length); break; } } } } }