Submission #233261

# Submission time Handle Problem Language Result Execution time Memory
233261 2020-05-20T07:45:26 Z anubhavdhar Type Printer (IOI08_printer) C++14
100 / 100
195 ms 99180 KB
#include<bits/stdc++.h>

#define ll long long int
#define pb push_back
#define mp make_pair
#define FOR(i,n) for(i=0;i<(n);++i)
#define FORe(i,n) for(i=1;i<=(n);++i)
#define FORr(i,a,b) for(i=(a);i<(b);++i)
#define FORrev(i,n) for(i=(n);i>=0;--i)
#define F0R(i,n) for(int i=0;i<(n);++i)
#define F0Re(i,n) for(int i=1;i<=(n);++i)
#define F0Rr(i,a,b) for(ll i=(a);i<(b);++i)
#define F0Rrev(i,n) for(int i=(n);i>=0;--i)
#define ii pair<ll,ll>
#define vi vector<ll>
#define vii vector<ii>
#define ff first 
#define ss second
#define cd complex<double>
#define vcd vector<cd>
#define ldd long double
#define dbgLine cout<<"Line : "<<__LINE__<<'\n'
#define all(x) (x).begin(),(x).end()

using namespace std;

const short int __PRECISION = 10;

const ll MOD = 1e9+7;
const ll INF = 1e17 + 1101;
const ll LOGN = 17;
const ll MAXN = 2e5+5;
const ll ROOTN = 320;

const ldd PI = acos(-1);
const ldd EPS = 1e-7;

const int Alp = 26;

int N, tmp;
vector<char> Answer;

struct node
{
	node * children[Alp];
	bool endOfWord;
	int dp;
};

node * getnode()
{
	node *p = new node;
	F0R(i, Alp)
		p->children[i] = NULL;
	p->endOfWord = false;
	return p;
}

node * root;

inline void insert(string S)
{
	node *p = root;
	for(char c : S)
	{
		if(p->children[c - 'a'] == NULL)
			p->children[c - 'a'] = getnode();
		p = p->children[c - 'a'];
	}
	p->endOfWord = true;
}

void dfsinit(node * p)
{
	p->dp = 1;
	F0R(i, Alp)
	{
		if(p->children[i] != NULL)
		{
			dfsinit(p->children[i]);
			// cout<<"calculated dp["<<(char)('a'+i)<<"] = "<<(p->children[i]->dp)<<'\n';
			p->dp = max(p->dp, p->children[i]->dp + 1);
		}
	}
}

void dfs(node * p)
{
	set<pair<int,int>> S;
	if(p->endOfWord)
		Answer.pb('P'), ++tmp;
	F0R(i, Alp)
	{
		if(p->children[i] != NULL)
		{
			S.insert(mp(p->children[i]->dp, i));

		}
	}
	for(pair<int, int> pp : S)
	{
		Answer.pb((char)(pp.ss + 'a'));
		dfs(p->children[pp.ss]);
	}
	if(tmp < N)
		Answer.pb('-');
}

int main()
{
	
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	cin>>N;
	tmp = 0;
	root = getnode();
	F0R(i, N)
	{
		string s;
		cin>>s;
		insert(s);
	}
	dfsinit(root);
	dfs(root);
	cout<<Answer.size()<<'\n';
	for(char c : Answer)
		cout<<c<<'\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 6 ms 1152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1920 KB Output is correct
2 Correct 8 ms 2304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 6016 KB Output is correct
2 Correct 28 ms 12544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 14720 KB Output is correct
2 Correct 13 ms 3328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 36468 KB Output is correct
2 Correct 156 ms 83432 KB Output is correct
3 Correct 90 ms 42864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 28532 KB Output is correct
2 Correct 195 ms 99180 KB Output is correct
3 Correct 102 ms 48624 KB Output is correct
4 Correct 163 ms 93552 KB Output is correct