Submission #1044078

# Submission time Handle Problem Language Result Execution time Memory
1044078 2024-08-05T07:08:47 Z Nonoze Type Printer (IOI08_printer) C++17
100 / 100
87 ms 67560 KB
/*
*	Author: Nonoze
*	Created: Monday 05/08/2024
*/
#include <bits/stdc++.h>
using namespace std;

#ifndef _IN_LOCAL
	#define dbg(...)
#endif

#define endl '\n'
#define endlfl '\n' << flush
#define quit(x) return (void)(cout << x << endl)

template<typename T> void read(T& x, bool write=0, bool write2=1, bool nothing=1) { if (write && write2) cout << x << endl; else if (write) cout << x << ' '; else cin >> x;}
template<typename T1, typename T2> void read(pair<T1, T2>& p, bool write=0, bool nothing=1, bool write2=1) { read(p.first, write, 0), read(p.second, write, 0); if (write && write2) cout << endl;}
template<typename T> void read(vector<T>& v, bool write=0) { for (auto& x : v) read(x, write, 0, 1); if (write) cout << endl; }
template<typename T1, typename T2> void read(T1& x, T2& y, bool write=0) { read(x, write), read(y, write); if (write) cout << endl; }
template<typename T1, typename T2, typename T3> void read(T1& x, T2& y, T3& z, bool write=0) { read(x, write), read(y, write), read(z, write); if (write) cout << endl; }
template<typename T1, typename T2, typename T3, typename T4> void read(T1& x, T2& y, T3& z, T4& zz, bool write=0) { read(x, write), read(y, write), read(z, write), read(zz, write); if (write) cout << endl; }
template<typename T> void print(T x) { read(x, 1); }
template<typename T1, typename T2> void print(T1 x, T2 y) { read(x, y, 1); }
template<typename T1, typename T2, typename T3> void print(T1 x, T2 y, T3 z) { read(x, y, z, 1); }
template<typename T1, typename T2, typename T3, typename T4> void print(T1 x, T2 y, T3 z, T4 zz) { read(x, y, z, zz, 1); }

#define sz(x) (int)(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b)

#define int long long
const int inf = numeric_limits<int>::max() / 4;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7, LOG=25;



void solve();

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int tt=1;
	// cin >> tt;
	while(tt--) solve();
	return 0;
}

string ans;

struct trie {
	map<char, trie*> children;
	int cnt=0;
	bool is_end = false;
	void add(string &x, int pos=0) {
		if (pos==sz(x)) {
			is_end = 1;
			cnt++;
			return;
		}
		if (!children.count(x[pos])) children[x[pos]] = new trie();
		children[x[pos]]->add(x, pos+1);
	}
	void print(string &lst, int pos=0, bool isin=1) {
		if (is_end) {
			while (cnt--) ans+='P';
		}
		for (auto u: children) {
			if (!isin || u.fi!=lst[pos]) {
				ans+=u.fi;
				u.se->print(lst, pos+1, 0);
				ans+='-';
			}
		}
		if (isin && pos<sz(lst) && children.count(lst[pos])) {
			ans+=lst[pos];
			children[lst[pos]]->print(lst, pos+1, 1);
		}
	}
};


int n, k, m, q;
vector<string> a;



void solve() {
	read(n);
	a.clear(), a.resize(n); read(a);
	trie *root = new trie();
	string mx="";
	for (auto &x: a) {
		if (sz(x)>sz(mx)) mx=x;
		root->add(x);
	}
	root->print(mx, 0);
	cout << sz(ans) << endl;
	for (auto u: ans) cout << u << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1372 KB Output is correct
2 Correct 2 ms 1624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4188 KB Output is correct
2 Correct 11 ms 8660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 10124 KB Output is correct
2 Correct 6 ms 2904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 24816 KB Output is correct
2 Correct 67 ms 56588 KB Output is correct
3 Correct 37 ms 30188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 19588 KB Output is correct
2 Correct 87 ms 67560 KB Output is correct
3 Correct 52 ms 34288 KB Output is correct
4 Correct 61 ms 63756 KB Output is correct