这里用到了STL中string求子串方法的sub_str()
然后就是按照字符串长度的因子从小到一个个比较了
sub_str用法如下(贴来的)
string substr(int start=0,int count= -1);
从起始位置开始复制字符串中的count 个字符,并返回这些字符作为子串。
如果字符串尾部小于count字符或者count 为-1,则字符串尾停止复制。
如果不使用参数调用只包括位置start,则substr()返回从位置开始到字符串尾部的子串。
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
string s;
int T;
cin>>T;
for(int t=0;t<T;t++){
if(t)
cout<<endl;
int flag=0;
cin>>s;
for(int factor=1;factor<=s.length();factor++){
if((s.length()%factor)==0){
string s1=s.substr(0,factor),s2;
for(int i=factor;i<s.length();i+=factor)
{
s2=s.substr(i,factor);
if(s2!=s1){
flag=1;
break;
}
}
if(flag==0){
cout<<factor<<endl;
break;
}
else
flag=0;
}//if s.length
}//factor
}//T--
return 0;
}