답안 #985280

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
985280 2024-05-17T14:26:43 Z roctes7 Type Printer (IOI08_printer) C++17
80 / 100
415 ms 80136 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
const double eps = 1e-6;
const int MAX = 3e5+3;

int adj[MAX][30];
int num[MAX];
int level[MAX];
int mx_level[MAX];
bool has[MAX];
int nex = 1;

void init(){
    memset(adj,-1,sizeof adj);
}

void add(string s){
	int i = 0, v = 0;
	while(i < s.size()){
		if(adj[v][s[i]-'a'] == -1)
			v = adj[v][s[i++]-'a'] = nex++;
		else
			v = adj[v][s[i++]-'a'];
	}
	has[v] = true;
	num[v]++;
}



void dfs(int u,int d){
    level[u] = d;
    for(char c='a';c<='z';c++)if(adj[u][c-'a']!=-1){
        dfs(adj[u][c-'a'],d+1);
    }
}


void dfs_mx(int u){
    if(has[u])mx_level[u] = level[u];
    for(char c='a';c<='z';c++)if(adj[u][c-'a']!=-1){
        dfs_mx(adj[u][c-'a']);
        mx_level[u] = max(mx_level[u],mx_level[adj[u][c-'a']]);
    }
}

vector<char> ans;

void dfs_ans(int u){
    if(has[u])ans.push_back('P');
    int mx = -1;
    int ind = -1;
    for(char c='a';c<='z';c++)if(adj[u][c-'a']!=-1)if(mx_level[adj[u][c-'a']]>0){
        if(mx_level[adj[u][c-'a']]>mx){
            mx = mx_level[adj[u][c-'a']];
            ind = adj[u][c-'a'];
        }
    }
    for(char c='a';c<='z';c++)if(adj[u][c-'a']!=-1)if(mx_level[adj[u][c-'a']]>0)if(adj[u][c-'a']!=ind){
        ans.push_back(c);
        dfs_ans(adj[u][c-'a']);
    }
    for(char c='a';c<='z';c++)if(adj[u][c-'a']!=-1)if(mx_level[adj[u][c-'a']]>0)if(adj[u][c-'a']==ind){
        ans.push_back(c);
        dfs_ans(adj[u][c-'a']);
    }

    ans.push_back('-');
}

int main(){
    fastio;
    //freopen("output.txt","w",stdout);


    init();

    int n;
    cin>>n;

    for(int i=0;i<n;i++){
        string s;
        cin>>s;
        add(s);
    }


    dfs(0,0);
    dfs_mx(0);
    dfs_ans(0);

    while(ans.back()=='-')ans.pop_back();
    cout<<ans.size()<<endl;

    for(auto a:ans)cout<<a<<endl;



    return 0;
}

Compilation message

printer.cpp: In function 'void add(std::string)':
printer.cpp:25:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |  while(i < s.size()){
      |        ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 39256 KB Output is correct
2 Correct 5 ms 39256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 39260 KB Output is correct
2 Correct 6 ms 39260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 39260 KB Output is correct
2 Correct 6 ms 39260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 39256 KB Output is correct
2 Correct 6 ms 39260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 39320 KB Output is correct
2 Correct 15 ms 39260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 39516 KB Output is correct
2 Correct 27 ms 39260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 39512 KB Output is correct
2 Correct 158 ms 40044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 167 ms 40148 KB Output is correct
2 Correct 52 ms 39512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 415 ms 40884 KB Output is correct
2 Runtime error 44 ms 80076 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 333 ms 40744 KB Output is correct
2 Runtime error 44 ms 80136 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -