输出字符串所有定长连续子串
1.1 题目描述
题目:
给定一个字符串,输出所有指定长度为n的子串,没有则输出-1
输入描述:
输入第一行一个字符串,如:“1234567890”
输入第二行一个数字是n,如5
输出描述:
输出所有长度为n的子串,如“12345”,“23456”,“34567”,
“45678”,“56789”
示例
输入
123456789
5
输出
12345 23456 34567 45678 56789
1.2 解答
1.2.1 题目解析
题目要求字符串长度为n的连续子串。题目较为简单,考察求子集。思路如下:
1. 输入字符串str和要求长度n;
2. 判断输入是否符合要求,即n不小于1,n不大于str长度;
3. 输入不合要求输出-1,结束;
4. 输入符合要求,循环截取字符串长度为n的连续子串并输出;
C++代码实现:
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
string str;
int n,len;
cin >> str;
cin >> n;
if(n<1 || n>str.size())
cout << -1 << endl;
else
for(int i=0;i<str.size()-n+1;i++)
{
cout << str.substr(i, n) << " ";
}
}
Java代码实现:
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
String a = sc.nextLine();
int len = sc.nextInt();
sc.close();
if(a.length()<len||len<1){
System.out.print("-1");
return;
}
for(int i=0;i<=a.length()-len;i++){
System.out.print(a.substring(i,i+len)+" ");
}
}
}