# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
746178 | khulegub | Type Printer (IOI08_printer) | C++17 | 22 ms | 21460 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>
using namespace std;
const int N = 100000;
/** Node count*/
int k = 1;
int to[N][26];
char a[N];
vector<char> ans;
void dfs(int u) {
if (u > 0) ans.push_back(a[u]);
bool is_leaf = true;
for (int i = 0; i < 26; i++) {
if (to[u][i] != -1) {
is_leaf = false;
dfs(to[u][i]);
}
}
if (is_leaf) {
ans.push_back('P');
}
ans.push_back('-');
}
int main() {
int n;
cin >> n;
memset(to, -1, sizeof to);
for (int i = 0; i < n; i++) {
string s;
cin >> s;
int curr = 0;
for (int j = 0; j < s.length(); j++) {
int c = s[j] - 'a';
if (to[curr][c] == -1) {
// Create a new node.
to[curr][c] = k;
k++;
}
a[to[curr][c]] = s[j];
curr = to[curr][c];
}
}
dfs(0);
vector<pair<int, string> > v;
string last = "";
int longest = -1;
int p_count = 0;
for (int i = 0; i < ans.size(); i++) {
if (ans[i] == '-') {
if (last != "") {
v.push_back({1, last});
last = "";
} else {
v.back().first++;
longest = max(longest, v.back().first);
}
} else {
if (ans[i] == 'P') {
p_count++;
}
last += (ans[i]);
}
}
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
cout << ans.size() - longest - p_count << endl;
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v[i].second.length(); j++) {
cout << v[i].second[j] << '\n';
}
if (i < v.size() -1) {
for (int j = 0; j < v[i].first - 1; j++) {
cout << '-' << '\n';
}
}
}
}
Compilation message (stderr)
# | 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... |