Submission #880672

# Submission time Handle Problem Language Result Execution time Memory
880672 2023-11-29T20:03:32 Z OAleksa Type Printer (IOI08_printer) C++14
100 / 100
103 ms 99784 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
vector<char> ans;
const int maxn = 5e5 + 69;
int trie[maxn][26], kraj[maxn], ban[maxn];
int n, node;
void add(string s) {
	int m = s.size();
	int t = 0;
	for (int i = 0;i < m;i++) {
		int x = s[i] - 'a';
		if (trie[t][x] == 0)
			trie[t][x] = ++node;
		t = trie[t][x];
	}
	kraj[t]++;
}
void dfs(int v) {
	for (int i = 0;i < kraj[v];i++)
		ans.push_back('P');
	int x = -1;
	for (int i = 0;i < 26;i++) {
		if (trie[v][i] && ban[trie[v][i]] != 1) {
			ans.push_back((char)(i + 'a'));
			dfs(trie[v][i]);
			ans.push_back('-');
		}
		else if (ban[trie[v][i]] == 1)
			x = i;
	}
	if (x != -1) {
		ans.push_back((char)(x + 'a'));
		dfs(trie[v][x]);
		ans.push_back('-');
	}
}
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
   int tt = 1;
  	//cin >> tt;
   while (tt--) {
   	cin >> n;
   	vector<string> a(n);
   	string mx;
   	for (int i = 0;i < n;i++) {
   		cin >> a[i];
   		if ((int)a[i].size() > (int)mx.size())
   			mx = a[i];
   		add(a[i]);
   	}
   	int t = 0;
   	for (int i = 0;i < (int)mx.size();i++) {
   		int x = mx[i] - 'a';	
   		t = trie[t][x];
   		ban[t] = 1;
   	}
   	dfs(0);
   	while (ans.back() == '-')
   		ans.pop_back();
   	cout << ans.size() << '\n';
   	for (auto x : ans)
   		cout << x << '\n';
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 3164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3928 KB Output is correct
2 Correct 2 ms 4184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9816 KB Output is correct
2 Correct 14 ms 16216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 20448 KB Output is correct
2 Correct 6 ms 5976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 39368 KB Output is correct
2 Correct 86 ms 84168 KB Output is correct
3 Correct 53 ms 48332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 31696 KB Output is correct
2 Correct 103 ms 99784 KB Output is correct
3 Correct 54 ms 53836 KB Output is correct
4 Correct 85 ms 94412 KB Output is correct