This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//for the question https://oj.uz/problem/view/IOI08_printer
#include <bits/stdc++.h>
using namespace std;
struct Trie
{
map<char,Trie*> children;
bool isWord;
int count;
void insertTrie(string s, int pos)
{
if(pos < s.size())
{
if(children.find(s[pos]) == children.end())
{
children[s[pos]] = new Trie;
children[s[pos]]->isWord = false;
children[s[pos]]->count = s.size() - pos;
}
int c = children[s[pos]]->count;
c = c > s.size() - pos ? c : s.size() - pos;
children[s[pos]]->count = c;
children[s[pos]]->insertTrie(s, pos+1);
}
else
{
isWord = true;
count = 0;
}
}
};
bool mysort(pair<char, Trie*> a, pair<char, Trie*> b)
{
return a.second->count <= b.second->count;
}
void doit(Trie * root, vector<char>& ans)
{
if(root->isWord == true)ans.push_back('P');
vector<pair<char,Trie*>> arr;
for(auto x : root->children)
{
arr.push_back({x.first, x.second});
}
sort(arr.begin(), arr.end(), mysort);
for(auto x : arr)
{
ans.push_back(x.first);
doit(x.second, ans);
ans.push_back('-');
}
}
int main()
{
int t;
cin >> t;
Trie mytrie;
mytrie.isWord = false;
while(t--)
{
string s;
cin >> s;
mytrie.insertTrie(s, 0);
}
vector<char> ans;
doit(&mytrie, ans);
int n = ans.size();
int count = n;
for(int i = n-1; i >= 0; i--)
{
if(ans[i] == '-')count--;
else break;
}
cout << count << endl;
for(int i = 0; i < count ; i++)cout << ans[i] << endl;
return 0;
}
Compilation message (stderr)
printer.cpp: In member function 'void Trie::insertTrie(std::string, int)':
printer.cpp:14:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | if(pos < s.size())
| ~~~~^~~~~~~~~~
printer.cpp:23:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | c = c > s.size() - pos ? c : s.size() - pos;
| ~~^~~~~~~~~~~~~~~~
# | 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... |