第一种算法:取子串一个一个一个一个比较
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void _4209_()
{
string str;
while (getline(cin,str))
{
if (str == ".")break;
int len = str.size();
if (len == 1)
{
cout << "1" << endl;
continue;
}
if (len == 0)
{
continue;
}
int ans = 0;
for (int i = 1;i <= len / 2;i++)//sub的长度
{
//获取sub
string sub = str.substr(0, i);
/*
for (int j = 0;j < i;j++)
{
sub[j] = str[j];
}*/
int count = 1;
for(int j = i;j < len;j += i)
{
//获取cmp
string cmp = str.substr(j,i);
if (sub != cmp)
{
count = 1;
break;
}
count++;
}
if (ans < count)ans = count;
}
cout << ans << endl;
}
}
int main()
{
_4209_();
return 0;
}
第二种算法:判断 (子串+子串+子串...... == 原str)
#include<iostream>
using namespace std;
string m;
int f(string a)
{
bool flag=false;
int ans;
for(int i=1;i<=a.size()&&!flag;i++)
{
string b=a.substr(0,i);
string res="";
if(a.size()%i==0)
{
for(int j=0;j<a.size()/i;j++)
res+=b;
if(res==a)
{
ans=a.size()/i;
flag=true;
}
}
}
return ans;
}
int main()
{
while(cin>> m,m!=".")
printf("%d\n",f(m));
return 0;
}