Submission #1091597

# Submission time Handle Problem Language Result Execution time Memory
1091597 2024-09-21T13:54:38 Z GuessWhoHas2Cats Type Printer (IOI08_printer) C++17
100 / 100
942 ms 50080 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()
{
    ios::sync_with_stdio(false);
	cin.tie(nullptr);
    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 1 ms 344 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 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 348 KB Output is correct
2 Correct 9 ms 908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 1256 KB Output is correct
2 Correct 18 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 3164 KB Output is correct
2 Correct 113 ms 6456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 7628 KB Output is correct
2 Correct 41 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 370 ms 18580 KB Output is correct
2 Correct 770 ms 41988 KB Output is correct
3 Correct 401 ms 22004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 278 ms 14776 KB Output is correct
2 Correct 942 ms 50080 KB Output is correct
3 Correct 467 ms 24824 KB Output is correct
4 Correct 908 ms 47116 KB Output is correct