Submission #635373

# Submission time Handle Problem Language Result Execution time Memory
635373 2022-08-26T07:16:17 Z NeroZein Type Printer (IOI08_printer) C++17
100 / 100
251 ms 101260 KB
#include <bits/stdc++.h>
#define FOR(i,a,b) for (int i=a; i<=(b); i++)
 
using namespace std;
int n;
long long ans;
string mx;
 
struct node{
    node* ch[26];
    int cnt; 
    node(){
        for(int i=0;i<26;i++)
            ch[i] = NULL;
        cnt = 0;
    }
};
 
node* root = new node();
void add(string s){
    node* cur = root; 
    for(int i=0;i<s.size();i++){
        int id = s[i]-'a';
        if (cur->ch[id] == NULL)
            cur->ch[id] = new node(),ans+=2;
        cur = cur->ch[id];
        cur->cnt++;
    }
}
set<string>se; 
void dfs(node* go,int lv=-1,bool keep = 1, string s=""){
    int left = -1;
    if (se.count(s))
        cout<<('P')<<'\n';
    for(int i=0;i<26;i++){
        if(go->ch[i] == NULL)continue;
        if(mx[lv+1]==(i+'a')&&keep){left=i;continue;}
        cout<<(char(i+'a'))<<'\n';
        dfs(go->ch[i],lv+1,0,s+(char(i+'a')));
        cout<<('-')<<'\n';
    }
    if (left!=-1){
        cout<<(char('a'+left))<<'\n';
        dfs(go->ch[left],lv+1,1,s+(char(left+'a')));
    }
}

signed main(){
 
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    cin>>n;
    FOR(i,1,n){
        string s; 
        cin>>s; 
        se.insert(s); 
        add(s);
        if((int)mx.size() < (int)s.size())
            mx = s; 
    }
    cout<<ans-(int)mx.size()+n<<'\n';
    dfs(root);
}

Compilation message

printer.cpp: In function 'void add(std::string)':
printer.cpp:22:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |     for(int i=0;i<s.size();i++){
      |                 ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 360 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 3 ms 1088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1876 KB Output is correct
2 Correct 4 ms 2260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 6056 KB Output is correct
2 Correct 34 ms 12804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 15140 KB Output is correct
2 Correct 17 ms 4308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 37468 KB Output is correct
2 Correct 205 ms 85196 KB Output is correct
3 Correct 124 ms 45136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 29980 KB Output is correct
2 Correct 251 ms 101260 KB Output is correct
3 Correct 124 ms 51148 KB Output is correct
4 Correct 197 ms 95804 KB Output is correct