#include <bits/stdc++.h>
#define int long long
using namespace std;
int pref(string s1, string s2) {
for (int i=0; i<s1.size(); i++) {
if (i>=s2.size() || s1[i]!=s2[i]) {
return i;
}
}
return min(s1.size(), s2.size());
}
signed main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n;
cin >> n;
vector<string> v;
int ma=0;
int mpos=-1;
for (int i=0; i<n; i++) {
string s;
cin >> s;
v.push_back(s);
if (s.size()>ma) {
ma=s.size();
mpos=i;
}
}
string mword=v[mpos];
sort(v.begin(), v.end(), [=](const string s1, const string s2) {
int x=pref(s1,mword);
int y=pref(s2,mword);
if (x!=y) {
return x<y;
}
return s1<s2;
});
int tot=0;
vector<char> prints;
string current;
for (int i=0; i<n; i++) {
while (current!=v[i].substr(0,current.size())) {
prints.push_back('-');
current.pop_back();
}
for (int j=current.size(); j<v[i].size(); j++) {
prints.push_back(v[i][j]);
}
current = v[i];
prints.push_back('P');
}
cout << prints.size() << endl;
for (auto i:prints) {
cout << i << endl;
}
}
Compilation message
printer.cpp: In function 'long long int pref(std::string, std::string)':
printer.cpp:6:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
6 | for (int i=0; i<s1.size(); i++) {
| ~^~~~~~~~~~
printer.cpp:7:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
7 | if (i>=s2.size() || s1[i]!=s2[i]) {
| ~^~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:26:21: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
26 | if (s.size()>ma) {
| ~~~~~~~~^~~
printer.cpp:49:37: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int j=current.size(); j<v[i].size(); j++) {
| ~^~~~~~~~~~~~
printer.cpp:41:9: warning: unused variable 'tot' [-Wunused-variable]
41 | int tot=0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
11 ms |
436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
344 KB |
Output is correct |
2 |
Correct |
22 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
724 KB |
Output is correct |
2 |
Correct |
133 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
177 ms |
1436 KB |
Output is correct |
2 |
Correct |
66 ms |
1164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
403 ms |
2684 KB |
Output is correct |
2 |
Correct |
875 ms |
4548 KB |
Output is correct |
3 |
Correct |
497 ms |
3864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
339 ms |
2580 KB |
Output is correct |
2 |
Execution timed out |
1060 ms |
5060 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |