题目描述
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "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).
输入
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.
输出
For each s you should print the largest n such that s = a^n for some string a.
样例输入
abcd
aaaa
ababab
.
样例输出
1
4
3
好像弄成英文看起来就变得高级了 其实就是求emmm.一时间我也不好描述..代码如下
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 int test(string s) 5 { 6 char a = s[0]; 7 int locate = s.find(a, 1); 8 while(locate > 0) 9 { 10 bool flag = false; 11 int len = s.length(); 12 int times = len/ locate; 13 if (len % locate == 0) 14 { 15 flag = true; 16 for (int i = locate;i < len&&flag==true;i += locate) 17 { 18 for (int j = 0;j <= locate - 1;j++) 19 { 20 if (s[j] != s[i + j]) 21 { 22 flag = false; 23 break; 24 } 25 } 26 27 } 28 29 } 30 if(flag) 31 return times; 32 else 33 locate = s.find(a, locate + 1); 34 } 35 return 1; 36 } 37 38 int main() 39 { 40 string s; 41 while (cin >> s && s != ".") 42 { 43 cout << test(s) << endl; 44 } 45 return 0; 46 }
这个做了我差不多5个小时..据说好像用什么KMP一些就做出来,现在还没学也看不懂,等到时候接触了再说吧