Submission #737947

# Submission time Handle Problem Language Result Execution time Memory
737947 2023-05-08T02:54:41 Z guagua0407 Type Printer (IOI08_printer) C++17
100 / 100
102 ms 56664 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

const int mxn=2.5e4*20+5;
int trie[mxn][26];
int cnt[mxn];
char c[mxn];
int cur=1;
vector<char> ans;
string mx="";

void dfs(int v,int d=0,bool tf=true){
    if(v!=0) ans.push_back(c[v]);
    if(cnt[v]) ans.push_back('P');
    for(int i=0;i<26;i++){
        if(trie[v][i]!=-1){
            if(tf and (mx[d]-'a')==i) continue;
            dfs(trie[v][i],d+1,false);
        }
    }
    if(tf and d<(int)mx.length()){
        dfs(trie[v][mx[d]-'a'],d+1,true);
    }
    if(v!=0) ans.push_back('-');
}

void insert(string str){
    int v=0,i=0;
    while(i<str.length()){
        if(trie[v][str[i]-'a']==-1){
            trie[v][str[i]-'a']=cur;
            v=cur;
            c[cur]=str[i];
            cur++;
        }
        else{
            v=trie[v][str[i]-'a'];
        }
        i++;
    }
    cnt[v]++;
}

int main() {_
    for(int i=0;i<mxn;i++){
        for(int j=0;j<26;j++){
            trie[i][j]=-1;
        }
    }
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        string str;
        cin>>str;
        insert(str);
        if(str.length()>mx.length()){
            mx=str;
        }
    }
    dfs(0);
    while(ans.back()=='-') ans.pop_back();
    cout<<(int)ans.size()<<'\n';
    for(auto v:ans){
        cout<<v<<'\n';
    }
    return 0;
}
//maybe its multiset not set

Compilation message

printer.cpp: In function 'void insert(std::string)':
printer.cpp:41:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |     while(i<str.length()){
      |           ~^~~~~~~~~~~~~
printer.cpp: In function 'void setIO(std::string)':
printer.cpp:12:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 51148 KB Output is correct
2 Correct 20 ms 51156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 51156 KB Output is correct
2 Correct 20 ms 51100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 51156 KB Output is correct
2 Correct 22 ms 51148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 51132 KB Output is correct
2 Correct 19 ms 51148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 51128 KB Output is correct
2 Correct 20 ms 51188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 51284 KB Output is correct
2 Correct 23 ms 51340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 51540 KB Output is correct
2 Correct 31 ms 51932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 52048 KB Output is correct
2 Correct 27 ms 51468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 53056 KB Output is correct
2 Correct 77 ms 55724 KB Output is correct
3 Correct 54 ms 53804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 52684 KB Output is correct
2 Correct 102 ms 56664 KB Output is correct
3 Correct 63 ms 54112 KB Output is correct
4 Correct 87 ms 56332 KB Output is correct