Submission #1091595

#TimeUsernameProblemLanguageResultExecution timeMemory
1091595GuessWhoHas2CatsType Printer (IOI08_printer)C++17
90 / 100
1024 ms49932 KiB
#include<bits/stdc++.h> using namespace std; const int N = 25000 * 20 + 10; int trie[N][26];// lvl[N]; int d[N]; int nodeNum; bitset<N> output; // This means that each edge of our trie will be used at least twice. Therefore a solution that uses each // edge exactly twice must be optimal. void add(string w) { int node = 0, l = 0; for(char c : w) { if(trie[node][c - 'a'] == 0) trie[node][c - 'a'] = ++nodeNum; node = trie[node][c - 'a']; // lvl[node] = ++l; } output[node] = 1; } string ans; // 要开数组记录全部的string已经是很大的空间里 // 再sort() O(nlogn) -> 而一次dfs才O(n) void dfs1(int u) { d[u] = 1; // leaf(output)的深度是1 for(int i = 0; i < 26; i ++) { int v = trie[u][i]; if(v > 0) { dfs1(v); d[u] = max(d[u], d[v] + 1); } } } void dfs2(int u) { // 出口 if(output[u]) { ans += "P"; // return; // 出口也是要print 1 个 "-" 的 } // 最后一个dfs的string可以省去 “-” 部分 // 把最长的string放到最后dfs for(int i = 0; i < 26; i ++) { int v = trie[u][i]; if(!v)continue; if(d[u] > d[v] + 1) { ans += 'a' + i; dfs2(v); } } // 要用循环 + 条件 选出最长 string 所在的path for(int i = 0; i < 26; i ++) { int v = trie[u][i]; if(!v)continue; if(d[u] == d[v] + 1) { ans += 'a' + i; dfs2(v); } } // 在调用完返回的时候加上 “-” ans += '-'; } int main() { int n; cin >> n; while(n --) { string w; cin >> w; add(w); } dfs1(0); dfs2(0); while (ans.back() == '-') { ans.pop_back(); } cout << ans.size() << endl; for(char c : ans) cout << c << endl; return 0; }

Compilation message (stderr)

printer.cpp: In function 'void add(std::string)':
printer.cpp:13:19: warning: unused variable 'l' [-Wunused-variable]
   13 |     int node = 0, l = 0;
      |                   ^
#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...