Submission #1091595

# Submission time Handle Problem Language Result Execution time Memory
1091595 2024-09-21T13:53:22 Z GuessWhoHas2Cats Type Printer (IOI08_printer) C++17
90 / 100
1000 ms 49932 KB
#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

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 time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 8 ms 880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 1116 KB Output is correct
2 Correct 19 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 3160 KB Output is correct
2 Correct 118 ms 6480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 150 ms 7556 KB Output is correct
2 Correct 42 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 351 ms 18492 KB Output is correct
2 Correct 885 ms 42296 KB Output is correct
3 Correct 450 ms 22004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 302 ms 14572 KB Output is correct
2 Execution timed out 1024 ms 49932 KB Time limit exceeded
3 Halted 0 ms 0 KB -