이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
#define endl '\n'
using namespace std;
using ll = long long;
const int N = 1e6+9;
const int B = 26;
int nxt[N][B], n, id;
string s, t;
vector<vector<char>> ans;
bool isl[N];
void add(string s)
{
int cur = 0;
for(auto &ch : s)
{
int x = ch - 'a';
if(nxt[cur][x] == 0)
nxt[cur][x] = ++id;
cur = nxt[cur][x];
}
isl[cur] = true;
}
void dfs(int u = 0)
{
if(isl[u])
ans.back().pb('P');
for(int i = 0; i < B; ++i)
{
if(nxt[u][i])
{
if(u == 0)
ans.emplace_back();
ans.back().pb('a'+i);
dfs(nxt[u][i]);
ans.back().pb('-');
}
}
}
int32_t main()
{
cin.tie(0)->sync_with_stdio(0);
cin>>n;
for(int i = 0; i < n; ++i)
cin>>s, add(s);
dfs();
vector<pair<int, int>> v;
int m = sz(ans);
for(int i = 0; i < m; ++i)
{
int j = sz(ans[i]) - 1;
while(j >= 0 && ans[i][j] == '-')
--j;
v.pb({sz(ans[i]) - j - 1, i});
}
sort(all(v));
vector<char> pt;
for(int i = 0; i < m-1; ++i)
for(auto &c : ans[v[i].second])
pt.pb(c);
int t = v.back().second;
int del = v.back().first;
for(int i = 0; i < sz(ans[t]) - del; ++i)
{
pt.pb({ans[t][i]});
}
cout<<sz(pt)<<endl;
for(auto &c : pt)
cout<<c<<endl;
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... |