This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// - 28/3/23
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <cmath>
#include <iterator>
#include <set>
#include <map>
#include <math.h>
#include <iomanip>
#include <unordered_set>
#include <queue>
#include <climits>
using namespace std;
// clang++ -std=c++17 IOI08_Printer.cpp && ./a.out
using ll = long long;
string max_str;
int get_pref(string a, string b){
int min_size = min((int)a.size(), (int)b.size());
int stop;
(a.size() == 0 || b.size() == 0) ? stop = 0 : stop = min_size;
// stop is length of same prefix
for (int i = 0; i < min_size; i++){
if (a[i] != b[i]){
stop = i;
break;
}
}
return stop;
}
bool comp(const string &a, const string &b){
int pref_len_a = get_pref(max_str, a);
int pref_len_b = get_pref(max_str, b);
return pref_len_a < pref_len_b;
}
void solve(){
int n;
cin >> n;
vector<string> a(n);
for (string& e : a) cin >> e;
int max_len = 0;
char end_ltr;
for (auto e : a){
if (e.size() > max_len){
max_len = e.size();
end_ltr = e[0];
max_str = e;
}
}
vector<string> first, last;
for (auto e : a){
if (e[0] == end_ltr) last.push_back(e);
else first.push_back(e);
}
sort(first.begin(), first.end());
sort(last.begin(), last.end(), comp); // SORT PREFIX HERE TOMROROWOWOOWO
a.clear();
for (auto e : first) a.push_back(e);
for (auto e : last) a.push_back(e);
// for (auto e : last) cout << e << ' ';
// cout << '\n';
vector<char> ans;
string s = "";
for (auto e : a){
// removing characters
int stop = get_pref(e, s);
// cout << s << ' ' << e << '\n';
// cout << stop << '\n';
for (int i = 0; i < s.size() - stop; i++) ans.push_back('-');
s = s.substr(0, stop);
// adding characters
while (s != e){
ans.push_back(e[s.size()]);
s += e[s.size()];
}
ans.push_back('P');
}
cout << ans.size() << '\n';
for (auto e : ans) cout << e << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
// freopen("input.txt", "r", stdin);
solve();
return 0;
}
Compilation message (stderr)
printer.cpp: In function 'void solve()':
printer.cpp:57:22: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
57 | if (e.size() > max_len){
| ~~~~~~~~~^~~~~~~~~
printer.cpp:93:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for (int i = 0; i < s.size() - stop; i++) ans.push_back('-');
| ~~^~~~~~~~~~~~~~~~~
printer.cpp:67:9: warning: 'end_ltr' may be used uninitialized in this function [-Wmaybe-uninitialized]
67 | if (e[0] == end_ltr) last.push_back(e);
| ^~
# | 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... |