Submission #1117287

# Submission time Handle Problem Language Result Execution time Memory
1117287 2024-11-23T08:28:08 Z Newtonabc Type Printer (IOI08_printer) C++14
100 / 100
148 ms 84924 KB
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10;
int trie[N][30];
bool word[N];
int cnt,sz[N],path[N];
stack<int> st;
vector<int> adj[N];
char type[N];
vector<char> pt;
void insert(string s){
    int u=0;
    for(int i=0;i<s.size();i++){
        if(trie[u][s[i]-'a']==0){
            trie[u][s[i]-'a']=++cnt;
            type[cnt]=s[i];
            adj[cnt].push_back(u);
            adj[u].push_back(cnt);
        }
        u=trie[u][s[i]-'a'];
    }
    word[u]=true;
}
void findsz(int u,int p){
    sz[u]=0;
    for(int i=0;i<adj[u].size();i++){
        int v=adj[u][i];
        if(v==p) continue;
        findsz(v,u);
        if(sz[v]+1>sz[u]){
            sz[u]=sz[v]+1;
            path[u]=v;
        }
    }
}
void dfs(int u,int p){
    while(!st.empty() && st.top()!=p) pt.push_back('-'),st.pop();
    if(u) st.push(u);
    if(u) pt.push_back(type[u]);
    if(word[u] && u) pt.push_back('P');
    for(int i=0;i<adj[u].size();i++){
        int v=adj[u][i];
        if(v==p || v==path[u]) continue;
        dfs(v,u);
    }
    if(sz[u]) dfs(path[u],u);

}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        string inp;
        cin>>inp;
        insert(inp);
    }
    findsz(0,0);
    dfs(0,0);
    cout<<pt.size() <<"\n";
    for(int i=0;i<pt.size();i++){
        cout<<pt[i] <<"\n";
    }
}

Compilation message

printer.cpp: In function 'void insert(std::string)':
printer.cpp:13:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |     for(int i=0;i<s.size();i++){
      |                 ~^~~~~~~~~
printer.cpp: In function 'void findsz(int, int)':
printer.cpp:26:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     for(int i=0;i<adj[u].size();i++){
      |                 ~^~~~~~~~~~~~~~
printer.cpp: In function 'void dfs(int, int)':
printer.cpp:41:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |     for(int i=0;i<adj[u].size();i++){
      |                 ~^~~~~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:60:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     for(int i=0;i<pt.size();i++){
      |                 ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12624 KB Output is correct
2 Correct 8 ms 12368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12368 KB Output is correct
2 Correct 7 ms 12536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12368 KB Output is correct
2 Correct 11 ms 12368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12624 KB Output is correct
2 Correct 9 ms 12368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12892 KB Output is correct
2 Correct 9 ms 12940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 13392 KB Output is correct
2 Correct 11 ms 13648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 16636 KB Output is correct
2 Correct 27 ms 21160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 23072 KB Output is correct
2 Correct 22 ms 14936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 39104 KB Output is correct
2 Correct 141 ms 73412 KB Output is correct
3 Correct 76 ms 44064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 32960 KB Output is correct
2 Correct 148 ms 84924 KB Output is correct
3 Correct 78 ms 48340 KB Output is correct
4 Correct 134 ms 80832 KB Output is correct