#include <algorithm>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P,int> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
const int M=500000;
int id=1,mp[M][26];
//map<char,int> mp[M];
int par[M];
bool used[M],vis[M];
int n;
string res;
int Add(string s){
int v=0;
for(auto c:s){
if(mp[v][c-'a']==-1){
mp[v][c-'a']=id;
par[id]=v;
id++;
}
v=mp[v][c-'a'];
}
vis[v]=true;
return v;
}
void dfs(int v){
if(vis[v]) res+='P';
char c_;
int u_=-1;
for(int i=0;i<26;i++) if(mp[v][i]>=0){
char c=(char)('a'+i);
int u=mp[v][i];
if(used[u]) c_=c,u_=u;
else{
res+=c;
dfs(u);
res+='-';
}
}
if(u_>=0){
res+=c_;
dfs(u_);
}
}
int main(){
for(int i=0;i<M;i++) for(int j=0;j<26;j++) mp[i][j]=-1;
par[0]=-1;
cin>>n;
int mx=0,v;
for(int i=0;i<n;i++){
string s;
cin>>s;
int j=Add(s);
if(s.size()>mx){
mx=s.size();
v=j;
}
}
while(v>=0){
used[v]=true;
v=par[v];
}
dfs(0);
cout<<res.size()<<endl;
for(auto c:res) cout<<c<<endl;
}
Compilation message
printer.cpp: In function 'int main()':
printer.cpp:89:14: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
89 | if(s.size()>mx){
| ~~~~~~~~^~~
printer.cpp:96:4: warning: 'v' may be used uninitialized in this function [-Wmaybe-uninitialized]
96 | v=par[v];
| ~^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
51200 KB |
Output is correct |
2 |
Correct |
33 ms |
51192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
51192 KB |
Output is correct |
2 |
Correct |
33 ms |
51200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
51192 KB |
Output is correct |
2 |
Correct |
33 ms |
51192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
51192 KB |
Output is correct |
2 |
Correct |
32 ms |
51192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
51192 KB |
Output is correct |
2 |
Correct |
49 ms |
51192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
51328 KB |
Output is correct |
2 |
Correct |
74 ms |
51320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
51576 KB |
Output is correct |
2 |
Correct |
286 ms |
51960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
336 ms |
52104 KB |
Output is correct |
2 |
Correct |
124 ms |
51448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
773 ms |
53264 KB |
Output is correct |
2 |
Execution timed out |
1097 ms |
54960 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
666 ms |
52756 KB |
Output is correct |
2 |
Execution timed out |
1096 ms |
55344 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |