#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;
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(p->endOfWord)
Answer.pb('P'), ++tmp;
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 |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Incorrect |
5 ms |
256 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
1792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
6016 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
47 ms |
14712 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
101 ms |
36464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
91 ms |
28528 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |