#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;
add(s);
tmp.push_back(s);
}
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;
| ~~~~~~~~~~^~~~~~
printer.cpp:63:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | 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 |
1 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
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 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1196 KB |
Output is correct |
2 |
Correct |
3 ms |
1488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
3636 KB |
Output is correct |
2 |
Correct |
19 ms |
7536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
8900 KB |
Output is correct |
2 |
Correct |
13 ms |
2620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
21900 KB |
Output is correct |
2 |
Correct |
138 ms |
48980 KB |
Output is correct |
3 |
Correct |
89 ms |
26556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
17464 KB |
Output is correct |
2 |
Correct |
148 ms |
58252 KB |
Output is correct |
3 |
Correct |
105 ms |
30004 KB |
Output is correct |
4 |
Correct |
134 ms |
55316 KB |
Output is correct |