Submission #531471

# Submission time Handle Problem Language Result Execution time Memory
531471 2022-02-28T19:01:08 Z Pietra Type Printer (IOI08_printer) C++14
20 / 100
7 ms 4916 KB
#include<bits/stdc++.h>
using namespace std ; 

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

const int maxn = 2e4 + 5 ; 

int n, trie[maxn][27], 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') ; 
		if(++ctt != n) resp.push_back('-') ; 
		return ; 
	}

	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) ; 
	}

}

int32_t main(){

	cin >> n ; 

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

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

	dfs(0, 0) ;

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

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

}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 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 too many deletions
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Incorrect 1 ms 300 KB too many deletions
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB too many deletions
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 1068 KB too many deletions
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 7 ms 4824 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 5 ms 4904 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 5 ms 4916 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 7 ms 4900 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -