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 long double ld;
typedef pair<int, int> pii;
const int MAX = 5e5 + 15;
const int ALP = 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
#define pb push_back
#define sz(x) (int) x.size()
#define fr first
#define sc second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define dbg(x) cerr << #x << ": " << "[ " << x << " ]\n"
int n, trie[MAX][ALP], mark[MAX], cnt, numWords;
bool fim[MAX];
string Maxs;
vector<char> ans;
void Add(string& s){
int cur = 0;
for(int i = 0; i < sz(s); i++){
int id = s[i] - 'a';
if(trie[cur][id] == 0)
trie[cur][id] = ++cnt;
cur = trie[cur][id];
}
fim[cur] = true;
}
void Done(){
cout << sz(ans) << '\n';
for(auto c : ans)
cout << c << '\n';
exit(0);
}
void Traverse(string& s){
int cur = 0;
for(int i = 0; i < sz(s); i++){
int id = s[i] - 'a';
cur = trie[cur][id];
mark[cur] = true;
}
}
void dfs(int u, char c = '*', bool f = false){
if(f) ans.pb(c);
if(fim[u]) ans.pb('P'), numWords++;
if(numWords == n) Done();
int left = -1;
char aux = '*';
for(char c = 'a'; c <= 'z'; c++){
int id = c - 'a';
if(trie[u][id] == 0)
continue;
if(mark[trie[u][id]])
left = trie[u][id], aux = c;
else
dfs(trie[u][id], c, true);
}
if(left != -1) dfs(left, aux, true);
if(f) ans.pb('-');
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for(int i = 0; i < n; i++){
string s; cin >> s;
Add(s);
if(sz(s) > sz(Maxs))
Maxs = s;
}
Traverse(Maxs);
dfs(0);
return 0;
}
# | 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... |