算法竞赛入门经典 第三章
注意判断周期的方法:
1.周期串的长度能被总长整除
2.利用string中的拼接,将周期串拼到原串的长度,再与原串比较
$\color{green}{UVa445-周期串-代码}$
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int kase;
cin >> kase;
string s, k;
while (kase--) {
cin >> s;
k = "";
int n = s.length();
for (int i = 0; i < n; i++) {
k += s[i];
if (n % k.length() == 0) {
int t = n / k.length();
string k2;
for (int j = 0; j < t; j++) k2 += k;//用当前认为的重复部分组成新字符串
if (k2 == s) {//如果新字符串与原字符串相同
cout << k.length() << endl;
if (kase) cout << endl;
break;
}
}
}
}
return 0;
}