#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 |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
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 |
5 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 |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
7 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1792 KB |
Output is correct |
2 |
Correct |
9 ms |
2304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
6016 KB |
Output is correct |
2 |
Correct |
36 ms |
12552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
14716 KB |
Output is correct |
2 |
Correct |
22 ms |
3456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
36468 KB |
Output is correct |
2 |
Correct |
206 ms |
83832 KB |
Output is correct |
3 |
Correct |
124 ms |
43248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
28532 KB |
Output is correct |
2 |
Correct |
239 ms |
99696 KB |
Output is correct |
3 |
Correct |
140 ms |
49136 KB |
Output is correct |
4 |
Correct |
214 ms |
94132 KB |
Output is correct |