#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 < s.size(); ++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 member function 'std::string Trie::query(std::string)':
printer.cpp:53:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | 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]);
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
332 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
332 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
1100 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
3404 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
8168 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
35 ms |
19924 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
35 ms |
15948 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |