#include<bits/stdc++.h>
using namespace std;
const int lmt=1e6;
int adj[lmt][26];
bool vis[lmt];
int subtree[lmt];
int indx=1;
int cnt=0;
string ans;
void insert(string s,int now)
{
for(char c:s)
{
int num=c-'a';
if(!adj[now][num])
{
indx++;
adj[now][num]=indx;
now=indx;
}
else
{
now=adj[now][num];
}
}
}
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 dfs(int u)
{
vector<pair<int,int>>p;
for(int i=0;i<26;i++)
{
if(adj[u][i]!=0)
{
p.push_back(make_pair(subtree[adj[u][i]],i));
}
}
sort(p.begin(),p.end());
for(int i=0;i<p.size();i++)
{
char c='a'+p[i].second;
ans+=c;
int v=adj[u][p[i].second];
dfs(v);
}
if(p.empty()) ans+='P';
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++)
{
string s;
cin>>s;
insert(s,1);
}
getsubtree(1);
dfs(1);
string res;
bool on=1;
reverse(ans.begin(),ans.end());
for(int i=0;i<ans.size();i++)
{
if(on and ans[i]=='-')
{
continue;
}
else
{
on=0;
res+=ans[i];
}
}
reverse(res.begin(),res.end());
cout<<res.size()<<endl;
for(char c:res)
{
cout<<c<<endl;
}
}
Compilation message
printer.cpp: In function 'void dfs(int)':
printer.cpp:51:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<p.size();i++)
~^~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:80:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<ans.size();i++)
~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 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 |
didn't print every 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 |
didn't print every word |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
384 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
1152 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
118 ms |
3196 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
280 ms |
7832 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
680 ms |
18744 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
559 ms |
14772 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |