#include <bits/stdc++.h>
using namespace std;
vector<string> tmp,v,u;
vector<char> order,ans;
int n,mxlen,ct,vis[5000005];
string s,mxs;
struct node{
int word,ac[30];
}trie[5000005];
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 c)
{
if (curr==0) dfs(trie[curr].ac[c-'a'],c);
else {
ans.push_back(c);
if (trie[curr].word) ans.push_back('P');
for (int i=0;i<26;i++){
if (trie[curr].ac[i]){
dfs(trie[curr].ac[i],char(i+'a'));
}
}
ans.push_back('-');
}
}
void final(int curr,char c,int lvl,bool ok=0)
{
//cout<<curr<<" "<<c<<" "<<lvl<<" "<<ok<<"\n";
if (curr==0) final(trie[curr].ac[c-'a'],c,lvl+1,0);
else {
ans.push_back(c);
vis[curr]=1;
if (trie[curr].word) ans.push_back('P');
for (int i=0;i<26;i++){
if (vis[trie[curr].ac[i]]) continue;
if (!trie[curr].ac[i]) continue;
if (ok) final(trie[curr].ac[i],char(i+'a'),lvl+1,1);
else {
if (!ok&&mxs[lvl]==char(i+'a')) continue;
final(trie[curr].ac[i],char(i+'a'),lvl+1,1);
}
}
if (mxs[lvl]!=' '&&!ok) {
//cout<<c<<" "<<lvl<<"\n";
if (!vis[trie[curr].ac[mxs[lvl]-'a']])
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);
}
//cout<<ct<<"\n";
mxs+=' ';
for (string i:tmp) if (i[0]!=mxs[0]) order.push_back(i[0]);
dfs(0,order[0]);
for (int i=1;i<order.size();i++) if (order[i]!=order[i-1]) dfs(0,order[i]);
final(0,mxs[0],0);
cout<<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:63:17: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
63 | if (s.length()>mxlen) mxlen=s.length(),mxs=s;
| ~~~~~~~~~~^~~~~~
printer.cpp:71:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for (int i=1;i<order.size();i++) if (order[i]!=order[i-1]) dfs(0,order[i]);
| ~^~~~~~~~~~~~~
printer.cpp:74:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i=0;i<ans.size()-mxlen;i++) cout<<ans[i]<<"\n";
| ~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
10 ms |
336 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Incorrect |
1 ms |
208 KB |
printed invalid word |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Incorrect |
1 ms |
208 KB |
printed invalid word |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
216 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
2496 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
550 ms |
20588 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1098 ms |
74188 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1092 ms |
86504 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1092 ms |
82224 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |