Submission #1118590

# Submission time Handle Problem Language Result Execution time Memory
1118590 2024-11-25T17:41:24 Z anuj_anand Type Printer (IOI08_printer) C++17
100 / 100
254 ms 84936 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 10 ms 12132 KB Output is correct
2 Correct 10 ms 12128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 12196 KB Output is correct
2 Correct 10 ms 12112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12112 KB Output is correct
2 Correct 10 ms 12200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 12284 KB Output is correct
2 Correct 9 ms 12112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 12112 KB Output is correct
2 Correct 11 ms 12640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 13148 KB Output is correct
2 Correct 15 ms 13664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 16220 KB Output is correct
2 Correct 29 ms 21260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 22708 KB Output is correct
2 Correct 22 ms 14416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 38708 KB Output is correct
2 Correct 156 ms 73164 KB Output is correct
3 Correct 96 ms 43608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 32960 KB Output is correct
2 Correct 254 ms 84936 KB Output is correct
3 Correct 108 ms 48072 KB Output is correct
4 Correct 141 ms 80844 KB Output is correct