Submission #782328

#TimeUsernameProblemLanguageResultExecution timeMemory
782328JustAmethystType Printer (IOI08_printer)C++17
100 / 100
227 ms50600 KiB
#include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x.size())
using namespace std;

const int N = 500000;
int edges[N][27];
vector<int> depth(N, -1);
int counter;

void dfs(int x) {
	if (edges[x][26]) {
		cout << "P\n";
		--counter;
	}
	for (int i = 0; i <= 20; ++i) {
		for (int j = 0; j < 26; ++j) {
			if (edges[x][j] != 0 && depth[edges[x][j]] == i) {
				cout << (char)('a'+j) << "\n";
				dfs(edges[x][j]);
			}
		}
	}
	if (counter > 0) cout << "-\n";
}

void solve() {
	int n, vs = 1, c;
	cin >> n;
	string s;
	for (int i = 0; i < n; ++i) {
		cin >> s;
		c = 0;
		for (int j = 0; j < sz(s); ++j) {
			if (edges[c][s[j]-'a'] == 0) {
				edges[c][s[j]-'a'] = vs;
				++vs;
			}
			c = edges[c][s[j]-'a'];
		}
		depth[c] = 0;
		edges[c][26] = 1;
	}
	for (int i = vs-1; i >= 0; --i) {
		for (int j = 0; j < 26; ++j) {
			if (edges[i][j] != 0) {
				depth[i] = max(depth[i], depth[edges[i][j]]+1);
			}
		}
	}
	int ans = ((vs-1)*2)+n-depth[0];
	cout << ans << "\n";

	counter = n;
	dfs(0);

	return;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	solve();
	
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...