//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
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;
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
300 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
1108 KB |
Output is correct |
2 |
Runtime error |
3 ms |
2516 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
8 ms |
6740 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
19 ms |
16536 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
54 ms |
41072 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
43 ms |
32144 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |