This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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){
for(auto zz:ss){
int i=zz-'a';
if(trie.node[v].child[i]!=-1){
dfs(trie.node[v].child[i]);
ans.push_back('-');
}
}
ans.push_back('P');
return;
}
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |