Submission #983422

#TimeUsernameProblemLanguageResultExecution timeMemory
983422roctes7Type Printer (IOI08_printer)C++17
20 / 100
414 ms77208 KiB
#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)

printer.cpp: In function 'void add(std::string)':
printer.cpp:24:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |  while(i < s.size()){
      |        ~~^~~~~~~~~~
printer.cpp: In function 'void solve(int, char)':
printer.cpp:48:19: warning: array subscript has type 'char' [-Wchar-subscripts]
   48 |     if(has[v])ans[sta].push_back('P');
      |                   ^~~
printer.cpp:50:13: warning: array subscript has type 'char' [-Wchar-subscripts]
   50 |         ans[sta].push_back(c);
      |             ^~~
printer.cpp:53:21: warning: array subscript has type 'char' [-Wchar-subscripts]
   53 |     if(pa[v]!=0)ans[sta].push_back('-');
      |                     ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...