This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <map>
using namespace std;
vector<map<char,int>> trie;
vector<bool> fin;
vector<char> ans;
void add(string& s){
int u=0;
for(int i=0;i<s.size();i++){
if(trie[u].find(s[i])==trie[u].end()){
trie[u][s[i]]=trie.size();
map<char,int> mp;
trie.push_back(mp);
fin.push_back(0);
}
u=trie[u][s[i]];
}
fin[u]=1;
}
void print(int u,int depth,string& s,bool m){
if(fin[u])ans.push_back('P');
//cout<<s<<' ';
for(auto x:trie[u]){
if(x.first==s[depth]&&m)continue;
//cout<<x.first<<' '<<s[depth]<<'\n';
ans.push_back(x.first);
print(x.second,depth+1,s,false);
ans.push_back('-');
}
//if(m)cout<<u<<' '<<trie[u][s[depth]]<<'\n';
if(m&&depth<s.size()){
ans.push_back(s[depth]);
print(trie[u][s[depth]],depth+1,s,m);
}
}
int main(){
map<char,int> mp;
trie.push_back(mp);
fin.push_back(0);
int n,m=0;
string s,p;
cin>>n;
while(n--){
cin>>s;
if(s.size()>m){
m=s.size();
p=s;
}
add(s);
}
print(0,0,p,true);
cout<<ans.size()<<'\n';
for(auto x:ans)cout<<x<<'\n';
}
Compilation message (stderr)
printer.cpp: In function 'void add(std::string&)':
printer.cpp:10:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | for(int i=0;i<s.size();i++){
| ~^~~~~~~~~
printer.cpp: In function 'void print(int, int, std::string&, bool)':
printer.cpp:32:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | if(m&&depth<s.size()){
| ~~~~~^~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:46:20: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
46 | if(s.size()>m){
| ~~~~~~~~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |