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>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
using ll = long long;
template<class T> bool cmin(T &i,T j) { return i > j ? i = j, true : false; }
template<class T> bool cmax(T &i,T j) { return i < j ? i = j, true : false; }
constexpr int nax = 1e6;
int trie[nax][26];
bool is_longest[nax];
int ending[nax];
string ans;
int par[nax];
int depth[nax];
void dfs(int x) {
while(ending[x]--) {
ans+="P";
}
for (int i=0;i<26;i++) {
if (!trie[x][i]||is_longest[trie[x][i]]) { continue; }
ans+=char(i+'a');
dfs(trie[x][i]);
}
for (int i=0;i<26;i++) {
if (trie[x][i]&&is_longest[trie[x][i]]) {
ans+=char(i+'a');
dfs(trie[x][i]);
}
}
if (!is_longest[x]) {
ans+="-";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
int cnt=1;
par[0]=-1;
while(n--) {
string s;
cin >> s;
int x=0;
for (auto &c: s) {
if (!trie[x][c-'a']) {
trie[x][c-'a']=cnt;
par[cnt]=x;
depth[cnt]=depth[x]+1;
cnt++;
}
x=trie[x][c-'a'];
}
ending[x]++;
}
int x=(int)(max_element(depth,depth+nax)-depth);
while(x != -1) {
is_longest[x]=true;
x=par[x];
}
dfs(0);
cout << ans.size() << "\n";
for (auto &c: ans) {
cout << c << "\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... |