#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <utility>
#include <cmath>
#include <numeric>
#include <assert.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 200007;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
struct Trie
{
int t[M][28] = {0}, id = 1, cnt[M] = {0};
void insert(string s){
int cur = 0;
for(int i = 0 ; i < s.size(); ++i){
int c = s[i] - 'a';
if(!t[cur][c]) t[cur][c] = id++;
cur = t[cur][c];
++cnt[cur];
}
}
void del(string s){
int cur = 0;
for(int i = 0 ; i < s.size(); ++i){
int c = s[i] - 'a';
if(!t[cur][c]) t[cur][c] = id++;
cur = t[cur][c];
--cnt[cur];
}
}
string query(string s){
string ret = "";
int cur = 0;
for(int i = 0; i < 22; ++i){
int c = s[i] - 'a';
if(cnt[t[cur][c]]) cur = t[cur][c], ret += char('a' + c);
else{
for(int i = 0; i < 26; ++i){
if(cnt[t[cur][i]]){
cur = t[cur][i];
ret += char('a' + i);
}
}
}
}
return ret;
}
}T;
int n;
string cur;
vector <char> ans;
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i){
string s;
cin >> s;
if(cur == "" || s.size() > cur.size() || (s.size() == cur.size() && s > cur)) cur = s;
T.insert(s);
}
T.del(cur);
vector <string> v;
v.pb(cur);
for(int i = 2; i <= n; ++i){
v.pb(T.query(v.back()));
T.del(v.back());
}
cur = "";
for(int i = n - 1; i >= 0; --i){
while(cur.size() > v[i].size() || v[i].substr(0, (int)cur.size()) != cur) cur.pop_back(), ans.pb('-');
for(int j = cur.size(); j < v[i].size(); ++j) cur += v[i][j], ans.pb(v[i][j]);
ans.pb('P');
}
cout << ans.size() << endl;
for(auto i : ans) cout << i << endl;
return 0;
}
Compilation message
printer.cpp: In member function 'void Trie::insert(std::string)':
printer.cpp:32:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int i = 0 ; i < s.size(); ++i){
| ~~^~~~~~~~~~
printer.cpp: In member function 'void Trie::del(std::string)':
printer.cpp:42:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int i = 0 ; i < s.size(); ++i){
| ~~^~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:93:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for(int j = cur.size(); j < v[i].size(); ++j) cur += v[i][j], ans.pb(v[i][j]);
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
printed invalid word |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
20 ms |
23852 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
24 ms |
24524 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
27 ms |
26944 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
39 ms |
31700 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
52 ms |
43388 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
68 ms |
16580 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |