-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path#57.cpp
More file actions
48 lines (40 loc) · 857 Bytes
/
#57.cpp
File metadata and controls
48 lines (40 loc) · 857 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
vector<string> *splitSentence(int k, vector<string> &tokens) {
vector<string> *sentences = new vector<string>;
string curr = "";
int numTokens = 0;
for (string token : tokens) {
if (curr.length() + token.length() + numTokens > k) {
sentences->push_back(curr);
curr = token;
numTokens = 1;
} else {
if (numTokens > 0) curr += " ";
curr += token;
numTokens++;
}
}
sentences->push_back(curr);
return sentences;
}
int main() {
string inp;
cin >> inp;
int k = stoi(inp);
while (getline(cin, inp)) {
stringstream ss(inp);
vector<string> v;
string token;
while (ss >> token) {
v.push_back(token);
}
vector<string>* sentences = splitSentence(k, v);
for (string s : *sentences) {
cout << s << endl;
}
}
}