1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| #include<bits/stdc++.h> #define ll long long #define ull unsigned long long #define maxn 50005 #define put() putchar('\n') using namespace std; inline void read(int &x){ int f=1;x=0;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();} x*=f; } int cnt=1,n,root=1; struct yyy{ int nex[26],flag,v; }a[maxn*20]; int maxlen;string Maxs; inline void insert(string s) { int i,now=root,len=s.size(); if (maxlen<len||(maxlen==len&&s>Maxs)) maxlen=len,Maxs=s; for (i=0;i<len;i++) { if (!a[now].nex[s[i]-'a']) a[now].nex[s[i]-'a']=++cnt; now=a[now].nex[s[i]-'a']; } a[now].flag++; }
inline void update(string s) { int i,now=root,len=s.size(); for (i=0;i<len;i++) { now=a[now].nex[s[i]-'a']; a[now].v++; } } char ans[2000005];int tot; inline void solve(int now) { if (a[now].flag) ans[++tot]='P'; int tmp=-1; for (int i=0;i<=25;i++) { if (a[now].nex[i]) { if (a[a[now].nex[i]].v==1) assert(tmp==-1),tmp=i; else ans[++tot]=('a'+i),solve(a[now].nex[i]),ans[++tot]='-'; } } if (tmp!=-1) ans[++tot]=('a'+tmp),solve(a[now].nex[tmp]); } signed main(void){ int i; string s; read(n); for (i=1;i<=n;i++) { cin>>s,insert(s); } update(Maxs); solve(root); printf("%d\n",tot);for (i=1;i<=tot;i++) putchar(ans[i]),put(); return 0; }
|