#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n,k;
int t;
int main(){
scanf("%d%d",&n,&k);
int cnt = 0;
while(n--){
string word;
cin >> word;
if(cnt + word.size() <= k){
if(cnt){ //每行第一个单词不控制空格输出,若下个单词可在该行输出时,则输出上个单词后的空格
cout << " ";
}
cout << word;
cnt += word.size();
}else{
cout << endl;
cnt = word.size();
cout << word;
}
}
return 0;
}
有一说一,只要对第一次读入进行特判即可,毕竟后面的读入已保证该行有一个单词
这题卡空格输出,琢磨了好久,题解有的都是错的…还得是y总的代码