#include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x).size()
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//uniform_int_distribution<int>(1000,10000)(rng)
int nextL[500000+5][26],depths[500000+5],tot=0;
vector<pair<int,int>> children[500000+5];
string alpha="abcdefghijklmnopqrstuvwxyz",ans="";
void dfs(int cur)
{
for (int i=0;i<26;++i)
if (nextL[cur][i])
{
dfs(nextL[cur][i]);
children[cur].push_back({depths[nextL[cur][i]],i});
depths[cur]=max(depths[cur],depths[nextL[cur][i]]+1);
}
sort(children[cur].begin(), children[cur].end());
}
void addWord()
{
int cur=0;
string s;
cin>>s;
for (auto c : s)
{
if (!nextL[cur][c-'a'])
{
++tot;
nextL[cur][c-'a']=tot;
}
cur=nextL[cur][c-'a'];
}
}
void ansBuilder(int cur)
{
for (auto i : children[cur])
{
ans+=alpha[i.second];
ansBuilder(nextL[cur][i.second]);
}
if (!sz(children[cur]))
ans+='P';
if (cur)
ans+="-";
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin>>n;
while (n--)
addWord();
dfs(0);
ansBuilder(0);
while (sz(ans)&&ans.back()=='-')
ans.pop_back();
cout<<sz(ans)<<"\n";
for (auto c : ans)
cout<<c<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12160 KB |
Output is correct |
2 |
Correct |
12 ms |
12160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
12032 KB |
Output is correct |
2 |
Correct |
12 ms |
11904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
12160 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12160 KB |
Output is correct |
2 |
Incorrect |
12 ms |
12032 KB |
didn't print every word |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
12160 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
13056 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
15616 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
21136 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
83 ms |
34708 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
81 ms |
29588 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |