# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
198778 | arnold518 | Type Printer (IOI08_printer) | C++14 | 132 ms | 41716 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;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 25000;
int N;
string S;
vector<char> ans;
struct Node
{
int dep;
Node *chd[30];
Node()
{
int i, j;
for(i=0; i<26; i++) chd[i]=NULL;
}
void update(string &S)
{
if(S.empty()) return;
char c=S.back(); S.pop_back();
if(chd[c-'a']==NULL) chd[c-'a']=new Node();
chd[c-'a']->update(S);
}
void calc()
{
int i, j;
for(i=0; i<26; i++)
{
if(chd[i]==NULL) continue;
chd[i]->calc();
dep=max(dep, chd[i]->dep+1);
}
}
void dfs()
{
int i, j;
int cnt=0;
vector<int> V;
for(i=0; i<26; i++)
{
if(chd[i]==NULL) continue;
V.push_back(i);
}
sort(V.begin(), V.end(), [&](const int &p, const int &q) { return chd[p]->dep<chd[q]->dep; });
for(auto i : V)
{
cnt++; ans.push_back(i+'a');
chd[i]->dfs();
ans.push_back('-');
}
if(cnt==0) ans.push_back('P');
}
};
Node *root=new Node();
int main()
{
int i, j;
cin>>N;
for(i=1; i<=N; i++)
{
cin>>S;
reverse(S.begin(), S.end());
root->update(S);
}
root->calc();
root->dfs();
while(!ans.empty() && ans.back()=='-') ans.pop_back();
printf("%d\n", ans.size());
for(auto it : ans) printf("%c\n", it);
}
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... |