# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
983422 | roctes7 | Type Printer (IOI08_printer) | C++17 | 414 ms | 77208 KiB |
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 ll long long
#define fi first
#define se second
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
const double eps = 1e-6;
const int MAX = 3e5+3;
int adj[MAX][30];
int num[MAX];
bool has[MAX];
int pa[MAX];
int nex = 1;
void init(){
memset(adj,-1,sizeof adj);
}
void add(string s){
int i = 0, v = 0;
while(i < s.size()){
if(adj[v][s[i]-'a'] == -1)
v = adj[v][s[i++]-'a'] = nex++;
else
v = adj[v][s[i++]-'a'];
}
has[v] = true;
num[v]++;
}
void dfs_num(int x,int p){
pa[x] = p;
for(char c='a';c<='z';c++){
if(adj[x][c-'a']!=-1){
dfs_num(adj[x][c-'a'],x);
num[x] += num[adj[x][c-'a']];
}
}
}
vector<char> ans[30];
void solve(int v,char sta){
if(has[v])ans[sta].push_back('P');
for(char c='a';c<='z';c++)if(adj[v][c-'a']!=-1)if(num[adj[v][c-'a']]>0){
ans[sta].push_back(c);
solve(adj[v][c-'a'],sta);
}
if(pa[v]!=0)ans[sta].push_back('-');
}
int main(){
fastio;
//freopen("output.txt","w",stdout);
init();
int n;
cin>>n;
for(int i=0;i<n;i++){
string s;
cin>>s;
add(s);
}
dfs_num(0,-1);
for(char c='a';c<='z';c++)if(adj[0][c-'a']!=-1)if(num[adj[0][c-'a']]>0){
solve(adj[0][c-'a'],c-'a');
}
vector<char> finaans;
int mx = -1;
int mx_val = 0;
for(char c='a';c<='z';c++)if(adj[0][c-'a']!=-1)if(num[adj[0][c-'a']]>0){
int res = 0;
for(int i=ans[c-'a'].size()-1;i>=0;i--){
if(ans[c-'a'][i]!='-')break;
res++;
}
if(res>mx_val){
mx = c-'a';
mx_val = res;
}
}
for(char c='a';c<='z';c++)if(adj[0][c-'a']!=-1)if(num[adj[0][c-'a']]>0)if(mx!=c-'a'){
finaans.push_back(c);
for(auto a:ans[c-'a'])finaans.push_back(a);
finaans.push_back('-');
}
finaans.push_back('a'+mx);
for(auto a:ans[mx])finaans.push_back(a);
while(finaans[finaans.size()-1]=='-')finaans.pop_back();
cout<<finaans.size()<<endl;
for(auto a:finaans)cout<<a<<endl;
return 0;
}
Compilation message (stderr)
# | 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... |