/*
Problems:
Author: AkiYuu
*/
#include <bits/stdc++.h>
#define task "GROUP"
#define ll long long
#define ld long double
#define pb(u) emplace_back(u)
#define ffw(i,a,b) for (ll i = a; i <= b; i++)
#define fbw(i,b,a) for (ll i = b; i >= a; i--)
#define adj(v,adj,u) for (auto v : adj[u])
#define rep(i,a) for (auto i : a)
#define reset(a) memset(a, 0, sizeof(a))
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
using namespace std;
void fastio(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen(task".inp", "r", stdin);
// freopen(task".out", "w", stdout);
}
const int mxn = 1e6 + 5;
typedef pair<ll, ll> ii;
int n;
int trie[25000 * 20 + 5][26], dem;
bool isend[25000 * 20 * 26];
ii longest[25000 * 20 * 26];
void add(string s){
int node = 0;
for (char c : s ){
int x = c - 'a';
if ( trie[node][x] == 0 ) trie[node][x] = ++dem;
node = trie[node][x];
}
isend[node ] = true;
}
int calc(int u){
longest[u] = ii(1,0);
ffw(i,0,25){
if ( trie[u][i] == 0 ) continue;
int v = trie[u][i];
longest[u] = max( longest[u] , ii(calc(v) + 1, v) );
}
return longest[u].first;
}
vector < char > path;
void dfs(int u){
if ( isend[u] ) {
path.pb('P');
return;
}
char LG;
ffw(i,0,25){
if ( trie[u][i] == 0) continue;
int v = trie[u][i];
if ( v == longest[u].second) {
LG = (char) ('a' + i);
continue;
}
path.pb( (char) ('a' + i) );
dfs(v);
path.pb( '-' );
}
path.pb(LG);
dfs(longest[u].second);
path.pb('-');
}
void solve(){
cin >> n;
ffw(i,1,n){
string s;
cin >> s;
add(s);
}
calc(0);
dfs(0);
while (sz(path) && path.back() == '-' ) path.pop_back();
cout << sz(path) << '\n';
for (char c : path ) cout << c << '\n';
}
int main(){
fastio();
int t = 1;
// cin >> t;
while (t--)
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
332 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Incorrect |
1 ms |
324 KB |
didn't print every word |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1100 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
3148 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
7756 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
19204 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
15032 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |