您的位置:首页 > 其它

蓝桥杯 算法提高 现代诗如蚯蚓

2018-01-05 16:12 246 查看
本题使用C++的substr方法写会十分方便

ac代码如下

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

int main()
{
string str;
cin>>str;
int max1=0;
int count;
for(int i=1;i<=str.length();i++){
count=1;
string sub_str=str.substr(0,i);
if(str.length()%i!=0)continue;
for(int j=i;j<str.length();j+=i){
string sub_str1=str.substr(j,i);
if(sub_str==sub_str1)count++;
}
if(count==str.length()/i)max1=max(count,max1);
}
cout<<max1;
}
顺便转载一下string的substr用法

substr有2种用法:
假设:string s = "0123456789";

string sub1 = s.substr(5); //只有一个数字5表示从下标为5开始一直到结尾:sub1 = "56789"

string sub2 = s.substr(5, 3); //从下标为5开始截取长度为3位:sub2 = "567"

改用法转自 http://blog.csdn.net/liuchuo/article/details/54599840
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: