# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
689940 | gagik_2007 | Type Printer (IOI08_printer) | C++17 | 167 ms | 58304 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 <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <chrono>
#include <ctime>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <limits>
#include <iomanip>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <functional>
#include <random>
#include <cassert>
using namespace std;
typedef long long ll;
typedef long double ld;
#define ff first
#define ss second
const int K = 26;
class Vertex {
public:
int size;
bool word;
int to[K];
Vertex() {
fill(to, to + K, -1);
size = 0;
word = false;
}
};
ll ttt;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll N = 1000007;
ll n, m, k;
vector<Vertex>p(1);
vector<char>ans;
void add(const string& s) {
int v = 0;
for (auto ch : s) {
int c = ch - 'a';
if (p[v].to[c] == -1) {
p[v].to[c] = p.size();
p.emplace_back();
}
v = p[v].to[c];
}
p[v].word = true;
}
void calc_size(int v) {
p[v].size = 1;
for (int i = 0; i < K; i++) {
if (p[v].to[i] != -1) {
calc_size(p[v].to[i]);
p[v].size = max(p[v].size, p[p[v].to[i]].size + 1);
}
}
}
void dfs(int v) {
if (p[v].word) {
ans.push_back('P');
}
vector<pair<int, pair<int,int>>>d;
for (int i = 0; i < K; i++) {
if (p[v].to[i] != -1) {
d.push_back({ p[p[v].to[i]].size,{p[v].to[i],i} });
}
}
sort(d.begin(), d.end());
for (int i = 0; i < d.size(); i++) {
ans.push_back(d[i].ss.ss + 'a');
dfs(d[i].ss.ff);
ans.push_back('-');
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
add(s);
}
calc_size(0);
dfs(0);
while ((*ans.rbegin()) == '-') {
ans.pop_back();
}
printf("%d\n", ans.size());
for (auto x : ans) {
printf("%c\n", x);
}
return 0;
}
/// ---- - -------- ------ -------- -- - - -
/// Just a reminder. Ubuntu password is I O I
/// ---- - -------- ------ -------- -- - - -
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... |