#include<bits/stdc++.h>
using namespace std;
const int lmt=1e6;
int adj[lmt][26];
bool vis[lmt];
int depth[lmt];//int subtree[lmt];
int indx=1;
int cnt=0;
string ans;
string s;
void insert()
{
int now=1;
for(char c:s)
{
int num=c-'a';
if(adj[now][num]==0)
{
indx++;
adj[now][num]=indx;
now=indx;
}
else
{
now=adj[now][num];
}
}
vis[now]=1;
}
/*void getsubtree(int u)
{
subtree[u]=1;
for(int i=0;i<26;i++)
{
if(adj[u][i]!=0)
{
int v=adj[u][i];
getsubtree(v);
subtree[u]+=subtree[v];
}
}
}*/
void getdepth(int u)
{
depth[u]=1;
for(int i=0;i<26;i++)
{
if(adj[u][i]!=0)
{
getdepth(adj[u][i]);
depth[u]=max(depth[u],depth[adj[u][i]]+1);
}
}
}
void dfs(int u)
{
if(vis[u])
{
vis[u]=0;
ans+='P';
}
int sz=0;
vector<pair<int,int>>p;
for(int i=0;i<26;i++)
{
if(adj[u][i]!=0)
{
sz++;
p.push_back(make_pair(depth[adj[u][i]],i));
}
}
sort(p.begin(),p.end());
for(int i=0;i<sz;i++)
{
char c='a'+p[i].second;
ans+=c;
dfs(adj[u][p[i].second]);
}
ans+='-';
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("take.in","r",stdin);
// freopen("give.out","w",stdout);
//#endif
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>s;
insert();
}
getdepth(1);
dfs(1);
reverse(ans.begin(),ans.end());
while(ans[0]=='-') ans.erase(ans.begin());
reverse(ans.begin(),ans.end());
cout<<ans.size()<<endl;
for(char c:ans) cout<<c<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
384 KB |
Output is correct |
2 |
Correct |
21 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
1152 KB |
Output is correct |
2 |
Correct |
43 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
3196 KB |
Output is correct |
2 |
Correct |
234 ms |
6520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
271 ms |
7688 KB |
Output is correct |
2 |
Correct |
92 ms |
1912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
685 ms |
18720 KB |
Output is correct |
2 |
Execution timed out |
1088 ms |
41648 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
561 ms |
14568 KB |
Output is correct |
2 |
Execution timed out |
1095 ms |
49196 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |