Submission #1312609

#TimeUsernameProblemLanguageResultExecution timeMemory
1312609ubormaciType Printer (IOI08_printer)C++20
100 / 100
210 ms163412 KiB
#include <iostream>
#include <algorithm> // for sort, mainly
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <array>
#include <string>
#include <cstdio>
#include <iterator>
#include <unordered_set>
#include <cstdint> // for int64_t, int32_t, etc
#include <queue>
#include <stack>
#include <deque>
#include <numeric> // gcd, lcm
#include <fstream>
#include <bitset> // for bitset
#include <iomanip>
#include <cassert> // for set with custom ordering
#include <type_traits> // for set with custom ordering
#include <utility> // for swap, forward, etc
using namespace std;

#pragma GCC optimize("O2")
// #pragma GCC optimize("O1","O2","O3","Ofast","unroll-loops")
// #pragma GCC target("sse","sse2","sse3","sse4.1","sse4.2","avx","avx2","fma")

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

/*

notes:

int64_t
stoi(string s) -> string to int
to_string() -> int (or else) to string

vector declaration:
vector<ll> v(n, 0)
vector<vector<ll>> v(n, vector<ll>(n, 0));

{if statement} ? {truth value} : {false value}

#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif

cout << fixed << setprecision(n);

set with custom ordering
set<ll, decltype(&cmp)> qu(cmp);

*/

typedef int64_t ll;

const ll inf = 1e16;

vector<ll> cnt(1, 0);
vector<vector<ll>> tree(1, vector<ll>(26, -1));
vector<ll> curr(1, -1);

vector<bool> don(1, false); // done here
vector<ll> depth;
vector<ll> md; // maximum depth

vector<string> ans;

ll dfs1(ll c) {

	bool noch = true; // no children

	for(ll j = 0; j < 26; j++) {

		if(tree[c][j] != -1) {
			if(depth[tree[c][j]] == inf) {
				noch = false;
				depth[tree[c][j]] = depth[c] + 1;
				dfs1(tree[c][j]);
				md[c] = max(md[tree[c][j]], md[c]);
			}
		}
	}

	if(noch) {
		md[c] = depth[c];
	}

	return md[c];

}

void dfs2(ll c) {

	if(c != 0) {

		string h = "A";
		h[0] = char(curr[c] + 'a');
		ans.push_back(h);
	}

	if(don[c]) {
		ans.push_back("P");
	}

	vector<pair<ll,ll>> srt;

	for(ll j = 0; j < 26; j++) {

		if(tree[c][j] != -1) {
			srt.push_back({md[tree[c][j]], tree[c][j]});
			// has a child
			//dfs2(tree[c][j]);
			//cnt[c]--;
		}
	}

	sort(srt.begin(), srt.end());
	for(const auto & [d, n] : srt) {
		dfs2(n);
	}

	if(c != 0) {
		ans.push_back("-");
	}
	return;

}

void solve() {
	
	ll n;
	cin >> n;

	vector<string> s(n, "");
	for(ll i = 0; i < n; i++) {
		cin >> s[i];
	}

	ll nxt = 1;

	for(ll i = 0; i < n; i++) {
		string h = s[i];

		ll loc = 0;

		for(ll j = 0; j < h.length(); j++) {

			//cerr << "\nh=" << h << "; j=" << j << ", char=" << h[j];

			if(tree[loc][h[j] - 'a'] == -1) {
				tree[loc][h[j] - 'a'] = nxt;
				cnt.push_back(1);
				loc = nxt;
				nxt++;
				tree.push_back({vector<ll>(26, -1)});
				curr.push_back(h[j] - 'a');
				don.push_back(false);
			}else{
				cnt[tree[loc][h[j] - 'a']]++;
				loc = tree[loc][h[j] - 'a'];
			}

			if(j == h.length() - 1) {
				don[loc] = true;
			}
		}
	}

	// ez csak egy DFS!

	depth.assign(nxt, inf);
	md.assign(nxt, 0);
	depth[0] = 0;
	md[0] = dfs1(0);
	
	/*
	cerr << "\ndebug=";
	for(ll i = 0; i < tree.size(); i++) {
		cerr << "\nind=" << i << "; char=" << char(curr[i] + 'a') << "; cnt=" << cnt[i];
		cerr << "\ndepth=" << depth[i] << "; md=" << md[i];
		for(ll j = 0; j < 26; j++) {
			if(tree[i][j] != -1) {
				cerr << "\nchild of " << j << " is at " << tree[i][j];
			}
		}
	}
	*/
	

	dfs2(0);

	ll en = ans.size() - 1;
	for(ll i = en; i >= 0; i--) {
		if(ans[i] == "-") {
			en--;
		}else{
			break;
		}
	}
	
	cout << en+1 << "\n";
	for(ll i = 0; i <= en; i++) {
		cout << ans[i] << "\n";
	}
}

int main()
{
	std::ios_base::sync_with_stdio(false);
	//cin.tie(nullptr);
	//cout.tie(nullptr);

	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...