Submission #1084321

#TimeUsernameProblemLanguageResultExecution timeMemory
1084321HaciyevAlikType Printer (IOI08_printer)C++14
100 / 100
165 ms99608 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define oo 1000000000000000 const int mx = 1e6 + 5; int trie[mx][26], d[mx], cnt[mx]; int last, node; vector<char> v; bool f; pair<int,int> null = {-1,-1}; void insert(string s) { int cur = 0; for(int i = 0; i < s.size(); ++i) { if(!trie[cur][s[i]-'a']) { trie[cur][s[i]-'a'] = ++last; } cur = trie[cur][s[i]-'a']; } cnt[cur]++; } void dfsd(int u) { for(int i = 0; i < 26; ++i) { if(trie[u][i]) { dfsd(trie[u][i]); d[u] = max(d[u],d[trie[u][i]] + 1); } } } void dfsn(int u) { int cur = -1; for(int i = 0; i < 26; ++i) { if(trie[u][i]) { cur = max(cur,d[trie[u][i]]); } } for(int i = 0; i < 26; ++i) { if(trie[u][i] > 0 && d[trie[u][i]] == cur) { dfsn(trie[u][i]); } } if(cur == -1) { node = u; } } void dfs(int u) { if(f) return; while(cnt[u] > 0) { v.push_back('P'); cnt[u]--; } int cur = -1; for(int i = 0; i < 26; ++i) { if(!trie[u][i]) continue; cur = max(cur,d[trie[u][i]]); } pair<int,int> lastgo = null; for(int i = 0; i < 26; ++i) { if(!trie[u][i]) continue; if(d[trie[u][i]] == cur) lastgo = {trie[u][i],i}; } for(int i = 0; i < 26; ++i) { if(trie[u][i] && trie[u][i] != lastgo.first) { v.push_back(char('a' + i)); dfs(trie[u][i]); } } if(lastgo != null) { v.push_back(char('a' + lastgo.second)); dfs(lastgo.first); } if(u == node) { f = 1; } if(!f) v.push_back('-'); } signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for(int i = 1; i <= n; ++i) { string s; cin >> s; insert(s); } dfsd(0); dfsn(0); dfs(0); cout << v.size() << '\n'; for(char ch : v) { cout << ch << '\n'; } return 0; }

Compilation message (stderr)

printer.cpp: In function 'void insert(std::string)':
printer.cpp:15:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |     for(int i = 0; i < s.size(); ++i) {
      |                    ~~^~~~~~~~~~
#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...