# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1091595 | GuessWhoHas2Cats | Type Printer (IOI08_printer) | C++17 | 1024 ms | 49932 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;
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)
# | 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... |