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 F first
#define S second
#define SZ(x) int((x).size())
const char nl = '\n';
using ll = long long;
using namespace std;
const int N = 25100;
int nam = 0;
int n;
int trie[N][27];
int dp[N][27];
int dep[N][27];
bool stop[N];
void add(string s) {
int x = 0;
for (auto c : s) {
if (!trie[x][c-'a']) {
trie[x][c-'a'] = ++nam;
}
dep[x][c-'a'] = max(dep[x][c-'a'], SZ(s));
x = trie[x][c-'a'];
}
stop[x] = true;
}
vector<char> res;
int fine = 0;
void dfs(int x) {
if (stop[x]) {
res.push_back('P');
fine ++;
}
vector<int> topo(27, 0);
iota(topo.begin(), topo.end(), 0);
sort(topo.begin(), topo.end(), [&](int a, int b) {
return dp[x][a] < dp[x][b];
});
for (auto c : topo) {
if (!trie[x][c]) {
continue;
}
res.push_back(c + 'a');
dfs(trie[x][c]);
}
if (fine != n) {
res.push_back('-');
}
}
void verkefni() {
cin >> n;
string s[n];
for (int i = 0; i < n; i ++) {
cin >> s[i];
add(s[i]);
}
dfs(0);
cout << SZ(res) << nl;
for (auto el : res) {
cout << el << nl;
}
// for (int i = 0; i <= nam; i ++) {
// for (int j = 0; j < 27; j ++) {
// if (trie[i][j]) {
// cout << "[" << i << ", " << j << "] --> " << trie[i][j] << nl;
// cout << "[" << i << ", " << j << "] --> " << dep[i][j] << nl;
// }
// }
// }
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tst = 1;
// cin >> tst;
while (tst --) {
verkefni();
}
}
# | 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... |