# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
964371 | anango | Type Printer (IOI08_printer) | C++17 | 0 ms | 0 KiB |
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 <bits/stdc++.h>
#define int long long
using namespace std;
int pref(string s1, string s2) {
for (int i=0; i<s1.size(); i++) {
if (i>=s2.size() || s1[i]!=s2[i]) {
return i;
}
}
return min(s1.size(), s2.size());
}
signed main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n;
cin >> n;
vector<string> v;
int ma=0;
int mpos=-1;
vector<int> answers(n);
for (int i=0; i<n; i++) {
string s;
cin >> s;
v.push_back(s);
if (s.size()>ma) {
ma=s.size();
mpos=i;
}
}
string mword=v[mpos];
for (int i=0; i<n; i++) {
answers[i] = pref(v[i], mword);
}
vector<int> w;
for (int i=0; i<n; i++) {
w.push_back(i);
}
sort(w.begin(), w.end(), [&](const int s1, const int s2) {
if (answers[s1]!=answers[s2]) {
return answers[s1]<answers[s2];
}
return v[s1]<v[s2];
});
vector<string> v2(n);
for (int i=0; i<n; i++) {
v2[i] = v[w[i]];
}
int tot=0;
vector<char> prints;
string current;
for (int i=0; i<n; i++) {
int t=pref(current,v2[i]);
while (current.size()>t) {
prints.append('-');
current.pop_back();
}
for (int j=current.size(); j<v2[i].size(); j++) {
prints.append(v2[i][j]);
}
current = v2[i];
prints.push_back('P');
}
cout << prints.size() << endl;
for (auto i:prints) {
cout << i << endl;
}
}