标签:stream fine mod get %s define 字符 printf ret
题目链接:POJ - 2406
题目描述
输入
输出
样例输入
abcd aaaa ababab .
样例输出
1 4 3
题解:字符串hash,然后枚举子串长度,。模板套一下就可以
//#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #define pb push_back #define ll long long #define ull unsigned long long #define rank wepsdas #define PI 3.14159265 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #define eps 1e-7 using namespace std; const int base=23; const int N=1e6+100; const int mod=1e9+7; struct hash { char s[N]; ull h[N],qp[N]; ll p[N],len,val[N]; void init() { h[0]=val[0]=0; qp[0]=p[0]=1; len=strlen(s+1); for(int i=1;i<=len;i++) { qp[i]=qp[i-1]*base; h[i]=h[i-1]*base+s[i]; } } ull get_hash(int l,int r) { return h[r]-h[l-1]*qp[r-l+1]; } }ac; bool judge(int l) { ull tmp=ac.get_hash(1,l); for(int i=l+1;i<=ac.len;i+=l) { if(tmp!=ac.get_hash(i,i+l-1))return false; } return true; } int main() { while(scanf("%s",ac.s+1)) { ac.init(); if(ac.len==1&&ac.s[1]==‘.‘)break; // printf("%s",ac.s+1); //? for(int i=) int ans=-1; for(int i=1;i<=ac.len;i++) { if(ac.len%i==0) { if(judge(i)) { ans=i;break; } } } printf("%d\n",ac.len/ans); } return 0; }
Power Strings POJ - 2406,字符串hash
标签:stream fine mod get %s define 字符 printf ret
原文地址:https://www.cnblogs.com/lhclqslove/p/10322531.html