给定一个字符串,输出所有指定长度为n的子串,没有则输出-1
#include <iostream>
#include <string>
using namespace std;
int main(){
string str;
int n;
cin >> str;
cin >> n;
if(n > str.length() || n <= 0){
cout << -1 << endl;
return 0;
}
for(int i = 0; i <= str.length() - n; ++i){
string temp = str.substr(i, n);
cout << temp << ' ';
}
cout << endl;
return 0;
} #include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string str;
int n;
vector <string> s;
while(cin>>str)
{
s.clear();
cin>>n;
int length = str.size();
if(length<n || n<0)
cout << -1 << endl;
else
{
for(int i=0;i<=length-n;i++)
{
s.push_back(str.substr(i,n));
}
for(auto it = s.cbegin();it!=s.cend();it++)
{
cout << *it << " ";
}
}
}
return 0;
} #include <bits/stdc++.h>
using namespace std;
int main(){
string s;
int n;
while(cin>>s){
int l = s.length();
cin>>n;
if(n>l || n<1)
cout<<-1<<endl;
else{
for(int i=0;i<=l-n;i++){
if(i==l-n)
cout<<s.substr(i,n)<<endl;
else
cout<<s.substr(i,n)<<" ";
}
}
}
return 0;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
int n = scanner.nextInt();
int len = str.length();
// n小于等于0也输出-1
if (n > len || n <= 0) {
System.out.println(-1);
} else {
System.out.print(str.substring(0, n));
for (int i = 1; i <= len - n; i++) {
System.out.print(" " + str.substring(i, i + n));
}
}
}
}
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s=in.next();
int n=in.nextInt();
if(n<=0||n>s.length()) System.out.println(-1);
else{
for(int i=0,j=n;j<=s.length();i++,j++){
System.out.printf("%s ",s.substring(i,j));
}
}
}
} #考虑边界条件
import sys
sub_str = sys.stdin.readline().strip()
n = int(sys.stdin.readline().strip())
if n>0 and len(sub_str)>=n:
res = []
for i in range(len(sub_str)-n+1):
res.append(sub_str[i:i+n])
print(' '.join(res))
else:
print('-1') import java.io.*;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int n = Integer.parseInt(br.readLine());
if(str.length() < n || n <= 0){
System.out.println("-1");
return;
}
for(int i = 0;i <= str.length()-n;i++){
if(i == str.length()-n){
System.out.print(str.substring(i) + " ");
}else{
System.out.print(str.substring(i,i+n) + " ");
}
}
}
}