# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
717174 | aedmhsn | Type Printer (IOI08_printer) | C++17 | 168 ms | 68160 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;
#define A first
#define B second
#define MP make_pair
#define ms(a, x) memset(a, x, sizeof(a))
#define boost() ios_base::sync_with_stdio(false); cin.tie(0)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<long double, long double> pld;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
struct Vertex{
vector <int> nxt;
bool leaf=false;
int longest=0;
Vertex(){
nxt.resize(26, -1);
}
};
vector <Vertex> trie(1);
void add_string(string &s){
int v=0;
for(int i=0; i<s.size(); i++){
int c = s[i]-'a';
if(trie[v].nxt[c] == -1){
trie[v].nxt[c] = trie.size();
trie.emplace_back();
}
trie[v].longest = max(trie[v].longest, (int)s.size()-i-1);
v=trie[v].nxt[c];
}
trie[v].leaf=true;
}
vector <char> ans;
// so i think there is a couple of ideas
// we can maybe sort them by size then do the print function such that it marks the strings visited ?
// well this is wierd
//
void solve(int v){
if(trie[v].leaf)
ans.push_back('P');
vector <pii> nodes;
for(int i=0; i<26; i++){
if(trie[v].nxt[i] != -1){
nodes.push_back({trie[trie[v].nxt[i]].longest, i});
}
}
sort(nodes.begin(), nodes.end());
for(auto x:nodes){
ans.push_back(x.B+'a');
solve(trie[v].nxt[x.B]);
ans.push_back('-');
}
}
int main(){
int n;
cin >> n;
vector <string> a(n);
for(int i=0; i<n; i++){
cin >> a[i];
add_string(a[i]);
}
solve(0);
while(ans.back() == '-'){
ans.pop_back();
}
cout << ans.size() << '\n';
for(auto x:ans) cout << x << '\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... |