이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
const int mxn=25005;
const int ascii=26;
int t[mxn*ascii][ascii];
int nxt=1;
vector<string>s;
vector<char>op;
unordered_map<int,bool>mp;
int d[mxn*ascii];
void add(string s){
int i=0,v=0;
while(i<sz(s)){
d[v]=max(d[v],sz(s)-i);
if(t[v][s[i]-'a']){
v=t[v][s[i++]-'a'];
}
else{
v=t[v][s[i++]-'a']=nxt++;
}
}
mp[v]=1;
}
int ans=0;
void dfs(int v){
vp ord;
if(mp[v]){
op.pb('P');
ans++;
}
for(int i=0;i<26;i++){
if(t[v][i]){
ord.pb({d[t[v][i]],i});
}
}
sort(all(ord));
for(int i=0;i<sz(ord);i++){
op.pb(ord[i].S+'a');
ans++;
dfs(t[v][ord[i].S]);
op.pb('-');
ans++;
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n;
cin>>n;
for(int i=0;i<n;i++){
string temp;
cin>>temp;
add(temp);
}
dfs(0);
while(op.back()=='-'){
op.pop_back();
ans--;
}
cout<<ans<<'\n';
for(auto i:op)cout<<i<<'\n';
}
/*
input:
*/
# | 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... |