蓝桥杯 算法提高 现代诗如蚯蚓
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
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
相关文章推荐
- 蓝桥杯ADV-157——算法提高 现代诗如蚯蚓
- 蓝桥杯 算法提高 现代诗如蚯蚓
- 蓝桥杯 ADV-157算法提高 现代诗如蚯蚓
- 蓝桥杯 算法提高 现代诗如蚯蚓
- 蓝桥杯 ADV-223 算法提高 8-1因式分解
- 蓝桥杯算法提高——日期计算
- 蓝桥杯练习系统试题集 算法提高 ADV-147 学霸的迷宫
- 蓝桥杯 ADV-148算法提高 排队打水问题(贪心)
- 蓝桥杯 算法提高 P0102
- 蓝桥杯 算法提高VIP 素数求和(Java解题)
- 蓝桥杯 算法提高 队列操作 (坑)
- 蓝桥杯-算法提高(ADV-143 扶老奶奶过街)
- 蓝桥杯_算法提高_判断名次
- 蓝桥杯 ADV-239 算法提高 P0102 (进制转换) (java)
- 蓝桥杯 算法提高 身份证号码升级
- 蓝桥杯 ADV-78 算法提高 最长单词
- 蓝桥杯 算法提高 第二大整数
- 蓝桥杯 ADV-102 算法提高 单词个数统计
- 蓝桥杯 ADV-211 算法提高 2-2整数求和
- 蓝桥杯 算法提高 学霸的迷宫