题意
大致是找出一个字符串中的一个连续的最短的重复单位,就是找一个循环部分。
记录这题主要是想写那个循环串的思想,
题解: 一个长度为k的字符串,其周期必定在1~k,所以对于串中的每一个元素加上周期T,如果相等的次数加一,如果最终相等的次数等于串长,那么T就是串的一个循环体,第一个找到的T就是最小的。
代码:
#include <iostream>
#include <vector>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <iomanip>
#define endl '\n'
#define _for(i, a, b) for(int i = (a); i < (b); i ++ )
#define _rep(i, a, b) for(int i = (a); i <= (b); i ++ )
using namespace std;
int main ()
{
ios :: sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T; cin >> T;
while (T -- )
{
string s; cin >> s;
int st = 1, len = s.size();
while (1)
{
int t = 0;
_for(i, 0, len)
if (s[i] == s[(i + st) % len]) t ++;
if (t == len)
{
cout << st << endl;
break;
}
st ++;
}
if (T) cout << endl;
}
return 0;
}