Submission #226660

# Submission time Handle Problem Language Result Execution time Memory
226660 2020-04-24T17:13:01 Z tushar_2658 Type Printer (IOI08_printer) C++14
20 / 100
57 ms 69800 KB
#include "bits/stdc++.h"
using namespace std;
 
const int maxn = 25005;
string s[maxn];
int t[maxn * 20 * 26][26], cnt[maxn * 20 * 26];
int node = 0;
int par[maxn * 20 * 26], ed[maxn * 20 * 26];
 
void insert(string s){
	int mx = s.size();
	int cur = 0;
	for(auto c : s){
		if(!t[cur][c - 'a']){
			t[cur][c - 'a'] = ++node;
		}
		cnt[t[cur][c - 'a']] = max(cnt[t[cur][c - 'a']], mx);
		par[t[cur][c - 'a']] = cur;
		cur = t[cur][c - 'a'];
	}
	ed[cur] = 1;
}
 
vector<char> v;
 
void dfs(int cur){
	bool leaf = ed[cur];
	if(leaf){
		v.push_back('P');
		int x = par[cur];
		int prv = cur;
		while(x != -1){
			vector<int> vv;
			for(int i = 0; i < 26; ++i){
				if(t[x][i]){
					vv.push_back(i);
				}
			}
			if(vv.size() == 1){
				v.push_back('-');
				t[x][vv[0]] = 0;
			}else {
				for(int i = 0; i < 26; ++i){
					if(t[x][i] == prv){
						v.push_back('-');
						t[x][i] = 0;
						break;
					}
				}
				break;
			}
			prv = x;
			x = par[x];
		}
		return;
	}
	vector<pair<int, pair<int, int>>> vv;
	for(int i = 0; i < 26; ++i){
		if(t[cur][i]){
			vv.push_back(make_pair(cnt[t[cur][i]], make_pair(t[cur][i], i)));
		}
	}
	sort(vv.begin(), vv.end());
	for(auto i : vv){
		v.push_back((char)(i.second.second + 'a'));
		dfs(i.second.first);
	}
}
 
int main(int argc, char const *argv[])
{
//	freopen("in.txt", "r", stdin);
	int n;
	cin >> n;
	memset(par, -1, sizeof par);
	for(int i = 0; i < n; ++i){
		cin >> s[i];
		insert(s[i]);
	}
	dfs(0);
	while(v.back() == '-')v.pop_back();
	cout << v.size() << endl;
	for(auto i : v){
		cout << i << endl;
	}
 
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 30 ms 51968 KB Output is correct
2 Correct 28 ms 51968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 51960 KB Output is correct
2 Correct 28 ms 51968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 29 ms 52224 KB didn't print every word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 28 ms 52088 KB Output is correct
2 Incorrect 27 ms 51968 KB didn't print every word
# Verdict Execution time Memory Grader output
1 Incorrect 29 ms 52108 KB didn't print every word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 33 ms 52856 KB didn't print every word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 33 ms 54784 KB didn't print every word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 38 ms 59008 KB didn't print every word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 57 ms 69800 KB didn't print every word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 54 ms 65660 KB didn't print every word
2 Halted 0 ms 0 KB -