Power Strings
Given two strings a and b we define ab to be their concatenation. For example, if a = “abc” and b = “def” then ab = “abcdef”. If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = “” (the empty string) and a^(n+1) = a*(a^n).
Input
Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.
Output
For each s you should print the largest n such that s = a^n for some string a.
Sample Input
abcd
aaaa
ababab
.
Sample Output
1
4
3
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
题意就是求周期个数,这个一种类型的题,就粘下来记录一下
#include<iostream>
#include<vector>
using namespace std;
string s;
int n;
vector<int> q(string s)
{
int len=s.size();
vector<int>ans(len);
for(int i=1;i<len;i++){
int j=ans[i-1];
while(s[i]!=s[j]&&j>0)j=ans[j-1];
if(s[i]==s[j])j++;
ans[i]=j;
}
return ans;
}
int main()
{
ios::sync_with_stdio(0);
while(cin>>s)
{
if(s[0]=='.')break;
n=s.size();
vector<int>ans=q(s);
//从这里
int p=n-ans[n-1];
if(n%p!=0)cout<<'1'<<endl;
else cout<<(n/p)<<endl;
//到这里,求周期个数
}
return 0;
}