#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 tot, 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;
++tot;
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)
{
++tmp;
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');
if(tmp < tot)
Answer.pb('-');
}
int main()
{
/*
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
*/
int N;
cin>>N;
tot = 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 |
5 ms |
256 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 |
Incorrect |
5 ms |
384 KB |
printed invalid word |
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 |
384 KB |
printed invalid word |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
512 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
1920 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
21 ms |
6016 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
46 ms |
14840 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
99 ms |
36724 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
84 ms |
28784 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |