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;
const int N = 25010;
int child[N][26], dp[N], depth[N], par[N], stop[N];
vector<pair<int,char>> adj[N];
vector<char> ans;
int cnt = 0, n, Node;
string s;
void insertword(string word)
{
int node = 0;
for(auto k : word)
{
if(child[node][k - 'a'] == 0)
{
child[node][k - 'a'] = ++cnt;
adj[node].push_back({cnt, k});
par[cnt] = node;
}
node = child[node][k - 'a'];
}
stop[node]++;
while(node != 0) depth[par[node]] = max(depth[par[node]], depth[node] + 1), node = par[node];
}
bool mmb(pair<int,char> a,pair<int,char> b)
{
return depth[a.first] <= depth[b.first];
}
void DFS(int u)
{
if(stop[u]) ans.push_back('P');
for(auto v : adj[u])
{
Node = child[Node][v.second - 'a'];
ans.push_back(v.second);
DFS(v.first);
}
ans.push_back('-');
}
int32_t main()
{
// freopen("test.inp", "r" ,stdin);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> s;
insertword(s);
}
for(int i = 0; i <= cnt; i++)
sort(adj[i].begin(), adj[i].end(), mmb);
DFS(0);
while(ans.back() == '-') ans.pop_back();
cout << ans.size() <<'\n';
for(auto k : ans) cout << k<<'\n';
}
# | 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... |