UVA10298 Power Strings
求一个字符串由多少个重复的子串连接而成
先搞hash就好
const int N = 1e6 + 79;
char s[N];
int n;
char ch;
ull h[N], base = 131, hash[N];
inline ull H(int l, int r) {
return hash[r] - hash[l - 1] * h[r-l+1];
}
inline bool check(int len,ull num){
for(int l=1;l<=n;l+=len){
if(H(l,l+len-1)!=num) return false;
}
return true;
}
/*
ababab
.
*/
int main() {
h[0] = 1;
rep(i, 1, N-5) {
h[i] = h[i - 1] * base;
}
s[0] = ' ';
while(1) {
n = 0;
ch = gt();
if(ch == '.') return 0;
while(ch != '\n') {
s[++n] = ch;
ch = gt();
}
s[n + 1] = '\0';
hash[0] = 0;
rep(i, 1, n) {
hash[i] = hash[i - 1] * base + s[i];
}
rep(i, 1, n) {
if(n % i) continue;
if(check(i,H(1,1+i-1))) {
out(n/i, '\n');
break;
}
}
}
return 0;
}