#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int n, m;
int main()
{
cin >> n;
while (n--)
{
//string s;
cin >> m;
string s;
cin >> s;
int res =m;
for (int i = 1; i <= m; i++)
{
int ans = 0;
if (m%i!= 0)//如果不能整除说明
//无法拼接成正常的串,直接排除
continue;
string ss = s.substr(0, i);//从前往后截取
for (int j = 0, k = 0; j < m; j++)
{
if (s[j] != ss[k++]) ans++;
if (k >= i)k = 0;//当一段检查完成之后另k=0,检查另一段
if (ans >1) break;
//ans只有0,1才是合法的其他直接排除
}
if (ans <= 1)
res = min(res, i);
ans = 0;
ss = s.substr(m-i, i);//倒着截取
for (int j = 0, k = 0; j < m; j++)
{
if (s[j] != ss[k++]) ans++;
if (k >= i)k = 0;
if (ans >1) break;
}
if (ans <= 1)
res = min(res, i);
}
cout << res << endl;
}
return 0;
}
标签:string,int,cin,Substring,++,Nearly,ans,Repeating,include
From: https://blog.csdn.net/2302_80415058/article/details/137182070