#include "bits/stdc++.h"
using namespace std;
#define int long long
#define cebug(x) cerr << "[" << #x << "] = " << x << endl
const int N=5e4+5;
const int mod=1e9+7;
int n,res;
struct Trie{
struct Node{
int child[26];
int end;
}node[N];
void init(){
for(int i=0; i<N; i++) memset(node[i].child,-1,sizeof(node[i].child));
for(int i=0; i<N; i++) node[i].end=0;
}
int nx=1;
void insert(string s){
int v=0;
for(auto c:s){
if(node[v].child[c-'a']==-1) node[v].child[c-'a']=nx++;
v=node[v].child[c-'a'];
}
node[v].end=1;
}
};
Trie trie;
string ans,ss,s[N];
int pos,cnt2[26];
bool cmp(string a, string b){
return (int)a.size()<(int)b.size();
}
void dfs(int v){
if(trie.node[v].end){
bool ok=1;
int cnt=0;
for(auto zz:ss){
int i=zz-'a';
if(trie.node[v].child[i]!=-1){
ok=0;
dfs(trie.node[v].child[i]);
cnt++;
}
}
if(ok) for(int i=1; i<=cnt; i++) ans.push_back('-');
ans.push_back('P');
}
for(auto zz:ss){
int i=zz-'a';
if(trie.node[v].child[i]!=-1){
ans.push_back(zz);
dfs(trie.node[v].child[i]);
ans.push_back('-');
}
}
}
void sol(){
trie.init();
cin>>n;
for(int i=1; i<=n; i++) cin>>s[i];
sort(s+1,s+n+1,cmp);
for(int i=1; i<=n; i++){
for(int j=0; j<(int)s[i].size(); j++){
if(!cnt2[s[i][j]-'a']){
ss.push_back(s[i][j]);
cnt2[s[i][j]-'a']=1;
}
}
}
for(int i=1; i<=n; i++) trie.insert(s[i]);
dfs(0);
for(int i=(int)ans.size()-1; i>=0; i--){
if(ans[i]=='P'){
pos=i;
break;
}
}
cout<<pos+1<<endl;
for(int i=0; i<=pos; i++) cout<<ans[i]<<"\n";
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
int t=1;
//cin>>t;
while(t--){
sol();
}
cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12380 KB |
Output is correct |
2 |
Correct |
2 ms |
12380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
12380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
12536 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
12376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
12380 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
12632 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
13040 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
16 ms |
25144 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
21 ms |
25692 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
20 ms |
25344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |