답안 #880051

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
880051 2023-11-28T16:20:21 Z cpptowin Type Printer (IOI08_printer) C++17
100 / 100
149 ms 106512 KB
#include<bits/stdc++.h>
#define db double
#define fo(i,d,c) for(int i=d;i<=c;i++)
#define fod(i,c,d) for(int i=c;i>=d;i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb push_back
#define en cout<<"\n";
#define int long long
#define inf 1e9
#define pii pair<int,int>
#define vii vector<pii>
#define bit(i,j) ((i>>j)&1)
#define offbit(i,j) (i^(1<<j))
#define onbit(i,j) (i|(j<<1))
#define vi vector<int>
#define vvi vector<vector<int>>
#define checkfile(FiLeNaMe) { if(fopen(FiLeNaMe".inp","r")) freopen(FiLeNaMe".inp","r",stdin),freopen(FiLeNaMe".out","w",stdout); }
template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}
using namespace std;
struct node
{
    node *child[26];
    int num,maxh;
    node()
    {
        memset(child,0,sizeof child);
        num=maxh=0;
    }
};
node *root=new node();
void add(string s)
{
    node *p=root;
    for(char c:s)
    {
        if(p->child[c-'a']==0) p->child[c-'a']=new node();
        p=p->child[c-'a'];
    }
    p->num++;
}
string f;///ans :v
void dfs(node *u)
{
    fo(i,0,25)
    {
        if(u->child[i])
        {
            dfs(u->child[i]);
            u->maxh=max(u->maxh,u->child[i]->maxh+1);
        }
    }
}
void dfs_ans(node *u)
{
    if(u->num) fo(i,1,u->num) f+='P';
    int maxx=-1;
    fo(i,0,25)
    {
        if(u->child[i])
        {
            if(maxx==-1) maxx=i;
            else if(u->child[maxx]->maxh<u->child[i]->maxh) maxx=i;
        }
    }
    fo(i,0,25)
    {
        if(i==maxx) continue;
        if(u->child[i])
        {
            f+=(i+'a');
            dfs_ans(u->child[i]);
            f+='-';
        }
    }
    if(maxx!=-1)
    {
        f+=(maxx+'a');
        dfs_ans(u->child[maxx]);
        f+='-';
    }
}
void process()
{
    int n;
    cin>>n;
    fo(i,1,n)
    {
        string s;
        cin>>s;
        add(s);
    }
    dfs(root);
    dfs_ans(root);
    while(f.back()=='-') f.pop_back();
    cout<<f.size()<<"\n";
    for(char c:f) cout<<c<<"\n";
}
main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    process();
}

Compilation message

printer.cpp:102:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  102 | main()
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 2 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1884 KB Output is correct
2 Correct 3 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 6488 KB Output is correct
2 Correct 17 ms 13404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 15848 KB Output is correct
2 Correct 9 ms 3672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 39152 KB Output is correct
2 Correct 129 ms 89496 KB Output is correct
3 Correct 65 ms 46064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 30708 KB Output is correct
2 Correct 149 ms 106512 KB Output is correct
3 Correct 81 ms 52464 KB Output is correct
4 Correct 125 ms 100504 KB Output is correct