// Problem: Type Printer
// Contest: Virtual Judge - OJUZ
// URL: https://vjudge.net/problem/OJUZ-IOI08_printer
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+1;
int n,trie[N][30],tot=1;
bool sum[N],flag[N],last=0;
string s,ll,ans;
void insert(string a){
int p=1;
int len=a.size();
for(int i=0;i<len;i++){
int ch=a[i]-'a';
if(!trie[p][ch]) trie[p][ch]=++tot;
p=trie[p][ch];
}
sum[p]=true;
return;
}
void Mark(string a){
int p=1;
int len=a.size();
for(int i=0;i<len;i++){
int ch=a[i]-'a';
p=trie[p][ch];
flag[p]=true;
}
return;
}
void dfs(int now){
if(sum[now]) ans+='P';
int x=-1;
for(int i=0;i<26;i++){
int t=trie[now][i];
if(!t) continue;
if(flag[t]) x=i;
else{
ans+=(i+'a');
dfs(t);
}
}
if(x!=-1){
ans+=(x+'a');
dfs(trie[now][x]);
}
if(flag[now] && x==-1) last=1;
if(!last) ans+='-';
return;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>s;
insert(s);
if(ll.size()<s.size()) ll=s;
}
Mark(ll);
dfs(1);
int len=ans.size();
cout<<len<<endl;
for(int i=0;i<len;i++) cout<<ans[i]<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
12 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
1112 KB |
Output is correct |
2 |
Correct |
20 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
3572 KB |
Output is correct |
2 |
Correct |
127 ms |
7248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
163 ms |
8700 KB |
Output is correct |
2 |
Correct |
45 ms |
2136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
404 ms |
20576 KB |
Output is correct |
2 |
Correct |
874 ms |
47016 KB |
Output is correct |
3 |
Correct |
462 ms |
24328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
316 ms |
16108 KB |
Output is correct |
2 |
Execution timed out |
1018 ms |
55404 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |