输出指定长度子串
输出指定长度子串
http://www.nowcoder.com/questionTerminal/56c6fb8231a44ed8ab91ac231f7b2c63
题解:
考察点: 暴力
易错点:
从位置
开始,长度为
的字符串为
的大小可能为0
解法:
由于每次选取长度为n的字符串输出,同时结合易错点中所述,需要枚举
的值为
,建议使用
中
类里面的
函数输出结果比较方便,该函数第一个参数为子串开始位置,第二个参数为子串长度。注意当
的值大于
或者小于
时不合理
#include "bits/stdc++.h"
using namespace std;
string s;
int n;
int main()
{
cin>>s>>n;
int len=s.length();
if(n>len||n<1){
cout<<"-1"<<endl;
}else{
for(int i=0;i+n<=len;i++){
cout<<s.substr(i,n)<<" ";
}
cout<<endl;
}
return 0;
} 
