[每日一题]AcWing 1442. 单词处理器
输入样例:
10 7
hello my name is Bessie and this is my essay
输出样例:
hello my
name is
Bessie
and this
is my
essay
样例解释
第一行包含 7 个非空格字符,包括 “hello” 以及 “my”。
再加入 “name” 会使得第一行包含 11>7个非空格字符,所以这个单词会被放到下一行。
(双指针模拟)
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int N = 110; 7 string s[N]; 8 int sum[N]; 9 int main() 10 { 11 int n, m; 12 cin >> n >> m; 13 14 for(int i = 1; i <= n; i ++){ 15 cin >> s[i]; 16 sum[i] = sum[i-1] + s[i].size(); 17 } 18 19 for (int i = 1, j = 1; i <= n; ) 20 { 21 while(sum[j]-sum[i-1] <= m && j <= n){ 22 j ++; 23 } 24 for(int k = i; k < j; k ++){ 25 cout << s[k]; 26 if(k != j-1) cout << " "; 27 } 28 cout << endl; 29 i = j; 30 } 31 return 0; 32 }
本文来自博客园,作者:泥烟,CSDN同名, 转载请注明原文链接:https://www.cnblogs.com/Knight02/p/16030143.html