#include <bits/stdc++.h>
using namespace std;
vector<string> tmp,v,u;
vector<char> order,ans;
int n,mxlen,ct,vis[550005];
string s,mxs;
struct node{
int word,ac[30];
}trie[550005];
void add(string x)
{
int curr=0;
for (int i=0;i<x.length();i++){
if (!trie[curr].ac[s[i]-'a']) trie[curr].ac[s[i]-'a']=++ct;
curr=trie[curr].ac[s[i]-'a'];
}
trie[curr].word=1;
}
void dfs(int curr,char x)
{
ans.push_back(x);
if (trie[curr].word) ans.push_back('P');
for (int i=0;i<26;i++){
if (!trie[curr].ac[i]) continue;
dfs(trie[curr].ac[i],char(i+'a'));
}
ans.push_back('-');
}
void final(int curr,char x,int lvl,int ok)
{
ans.push_back(x);
if (trie[curr].word) ans.push_back('P');
for (int i=0;i<26;i++){
if (!trie[curr].ac[i]) continue;
if (ok) final(trie[curr].ac[i],char(i+'a'),lvl+1,ok);
else {
if (!ok&&mxs[lvl]==char(i+'a')) continue;
final(trie[curr].ac[i],char(i+'a'),lvl+1,1);
}
}
if (mxs[lvl]!=' '&&!ok){
final(trie[curr].ac[mxs[lvl]-'a'],mxs[lvl],lvl+1,0);
}
ans.push_back('-');
}
int main()
{
cin>>n;
for (int i=0;i<n;i++){
cin>>s;
if (s.length()>mxlen) mxlen=s.length(),mxs=s,tmp.clear();
else if (s.length()==mxlen){
tmp.push_back(s);
}
add(s);
tmp.push_back(s);
}
tmp.push_back(mxs);
sort(tmp.begin(),tmp.end());
mxs=tmp[tmp.size()-1];
mxs=mxs+' ';
for (int i=0;i<26;i++){
if (char(i+'a')==mxs[0]) continue;
if (!trie[0].ac[i]) continue;
dfs(trie[0].ac[i],char(i+'a'));
}
final(trie[0].ac[mxs[0]-'a'],mxs[0],1,0);
cout<<int(ans.size())-mxlen<<"\n";
for (int i=0;i<ans.size()-mxlen;i++) cout<<ans[i]<<"\n";
}
Compilation message
printer.cpp: In function 'void add(std::string)':
printer.cpp:13:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for (int i=0;i<x.length();i++){
| ~^~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:51:17: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
51 | if (s.length()>mxlen) mxlen=s.length(),mxs=s,tmp.clear();
| ~~~~~~~~~~^~~~~~
printer.cpp:52:22: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
52 | else if (s.length()==mxlen){
| ~~~~~~~~~~^~~~~~~
printer.cpp:69:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int i=0;i<ans.size()-mxlen;i++) cout<<ans[i]<<"\n";
| ~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
1100 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
3580 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
9028 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
74 ms |
21936 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
75 ms |
17464 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |