This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |