이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define mod 1000000007
template<typename T> bool chkmin(T &a, T b){return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chkmax(T &a, T b){return (b > a) ? a = b, 1 : 0;}
ll ksm(ll a, ll b) {if (b == 0) return 1; ll ns = ksm(a, b >> 1); ns = ns * ns % mod; if (b & 1) ns = ns * a % mod; return ns;}
using namespace std;
const int maxn = 25050;
const int maxm = maxn * 20;
int n;
int nxt[maxm][26], tsz;
int mx[maxm];
bool ed[maxm];
string s[maxn];
void add(string s) {
int node = 0;
int sz = (int) s.size();
vi vis;
for (int i = 0; i < sz; i++) {
int x = (s[i] - 'a');
if (!nxt[node][x]) nxt[node][x] = ++tsz;
node = nxt[node][x];
vis.pb(node);
}
ed[vis.back()] = true;
reverse(vis.begin(), vis.end());
for (int i = 0; i < (int) vis.size(); i++) {
if (i == 0) mx[vis[i]] = max(mx[vis[i]], 1);
else mx[vis[i]] = max(mx[vis[i]], mx[vis[i - 1]] + 1);
}
}
vector<char> ans;
void go(int node) {
if (ed[node]) ans.pb('P');
vector<pair<int, int>> sl;
for (int i = 0; i < 26; i++) {
if (nxt[node][i]) {
sl.pb(mp(mx[nxt[node][i]], i));
}
}
sort(sl.begin(), sl.end());
for (auto x : sl) {
ans.pb((char) (x.se + 'a'));
go(nxt[node][x.se]);
ans.pb('-');
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s[i];
add(s[i]);
}
go(0);
while (!ans.empty() && ans.back() == '-') {
ans.pop_back();
}
printf("%d\n", (int) ans.size());
for (char c : ans) {
printf("%c\n", c);
}
}
# | 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... |