#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define mod 1000000007
template<typename T> bool chkmin(T &a, T b){return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chkmax(T &a, T b){return (b > a) ? a = b, 1 : 0;}
ll ksm(ll a, ll b) {if (b == 0) return 1; ll ns = ksm(a, b >> 1); ns = ns * ns % mod; if (b & 1) ns = ns * a % mod; return ns;}
using namespace std;
const int maxn = 25050;
const int maxm = maxn * 20;
int n;
int nxt[maxm][26], tsz;
int mx[maxm];
bool ed[maxm];
string s[maxn];
void add(string s) {
int node = 0;
int sz = (int) s.size();
vi vis;
for (int i = 0; i < sz; i++) {
int x = (s[i] - 'a');
if (!nxt[node][x]) nxt[node][x] = ++tsz;
node = nxt[node][x];
vis.pb(node);
}
ed[vis.back()] = true;
reverse(vis.begin(), vis.end());
for (int i = 0; i < (int) vis.size(); i++) {
if (i == 0) mx[vis[i]] = max(mx[vis[i]], 1);
else mx[vis[i]] = max(mx[vis[i]], mx[vis[i - 1]] + 1);
}
}
vector<char> ans;
void go(int node) {
if (ed[node]) ans.pb('P');
vector<pair<int, int>> sl;
for (int i = 0; i < 26; i++) {
if (nxt[node][i]) {
sl.pb(mp(mx[nxt[node][i]], i));
}
}
sort(sl.begin(), sl.end());
for (auto x : sl) {
ans.pb((char) (x.se + 'a'));
go(nxt[node][x.se]);
ans.pb('-');
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s[i];
add(s[i]);
}
go(0);
while (!ans.empty() && ans.back() == '-') {
ans.pop_back();
}
printf("%d\n", (int) ans.size());
for (char c : ans) {
printf("%c\n", c);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1132 KB |
Output is correct |
2 |
Correct |
2 ms |
1484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1868 KB |
Output is correct |
2 |
Correct |
6 ms |
1996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
3992 KB |
Output is correct |
2 |
Correct |
22 ms |
7372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
8392 KB |
Output is correct |
2 |
Correct |
15 ms |
2932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
19676 KB |
Output is correct |
2 |
Correct |
143 ms |
44080 KB |
Output is correct |
3 |
Correct |
87 ms |
23912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
15420 KB |
Output is correct |
2 |
Correct |
162 ms |
52288 KB |
Output is correct |
3 |
Correct |
96 ms |
26944 KB |
Output is correct |
4 |
Correct |
159 ms |
49440 KB |
Output is correct |