Submission #531473

# Submission time Handle Problem Language Result Execution time Memory
531473 2022-02-28T19:09:49 Z Pietra Type Printer (IOI08_printer) C++14
30 / 100
42 ms 19700 KB
#include<bits/stdc++.h>
using namespace std ; 

//monta trie 
// percorre ela com dfs 
// chegou ao fim printa 
// qd sobe coloca - 

const int maxn = 6e5 + 5 ; 

int n, trie[maxn][28], ctt, qtd[maxn], fim[maxn], ct ; 
string s ; 
vector<char> resp ; 

void add(string s){

	int root = 0 ; 

	for(int i = 0 ; i < (int) s.size() ; i++){
		if(trie[root][s[i]-'a'] == 0) trie[root][s[i]-'a'] = ++ct ; 
		root = trie[root][s[i]-'a'] ; 
	}

	fim[root] = 1 ;

}

bool cmp(pair<char,int> a, pair<char,int> b){return qtd[trie[a.second][a.first-'a']] < qtd[trie[b.second][b.first-'a']] ; }

void dfs(int v, int p){

	if(fim[v]){
		resp.push_back('P') ; ctt++ ; 
	}

	vector<pair<char,int>> order ; 

	for(char i = 'a' ; i <= 'z' ; i++){
		if(trie[v][i-'a'] == 0 || trie[v][i-'a'] == p) continue ; 
		order.push_back({i, v}) ; 
	}

	sort(order.begin(), order.end(), cmp) ; 

	// cout << v << ":\n" ; 

	for(auto j : order){
		int i = j.first ; 
		// cout << j.first << " " << qtd[trie[v][j.first-'a']] << "\n" ;  
		resp.push_back(i) ;
		dfs(trie[v][i-'a'], v) ; 
	}

	if(ctt != n)  resp.push_back('-')  ; 

}

void dfs_h(int v, int p){

	qtd[v] = 1 ; 

	if(fim[v]) return ; 

	for(char i = 'a' ; i <= 'z' ; i++){
		if(trie[v][i-'a'] == 0 || trie[v][i-'a'] == p) continue ; 
		dfs_h(trie[v][i-'a'], v) ; 
		qtd[v] = max(qtd[v], qtd[trie[v][i-'a']] + 1) ; 
	}

}

int main(){

	ios_base::sync_with_stdio(false) ; cin.tie(NULL) ; 

	cin >> n ; 

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

	dfs_h(0, 0) ; // achar a maior alt de cada um 

	// for(int i = 0 ; i < 4 ; i++) cout << qtd[i] << " " ; 
	// cout << "\n" ; 

	dfs(0, 0) ;

	cout << resp.size() << "\n" ; 

	for(auto a : resp) cout << a << "\n" ; 

}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 1100 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 11 ms 3272 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 21 ms 8028 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 42 ms 19700 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 40 ms 15440 KB Output isn't correct
2 Halted 0 ms 0 KB -