# bzoj 3012 [Usaco2012 Dec]First!

### Description

Bessie has been playing with strings again. She found that by changing the order of the alphabet she could make some strings come before all the others lexicographically (dictionary ordering). For instance Bessie found that for the strings “omm”, “moo”, “mom”, and “ommnom” she could make “mom” appear first using the standard alphabet and that she could make “omm” appear first using the alphabet “abcdefghijklonmpqrstuvwxyz”. However, Bessie couldn’t figure out any way to make “moo” or “ommnom” appear first. Help Bessie by computing which strings in the input could be lexicographically first by rearranging the order of the alphabet. To compute if string X is lexicographically before string Y find the index of the first character in which they differ, j. If no such index exists then X is lexicographically before Y if X is shorter than Y. Otherwise X is lexicographically before Y if X[j] occurs earlier in the alphabet than Y[j].

### Input

• Line 1: A single line containing N (1 <= N <= 30,000), the number of strings Bessie is playing with.
• Lines 2..1+N: Each line contains a non-empty string. The total number of characters in all strings will be no more than 300,000. All characters in input will be lowercase characters ‘a’ through ‘z’. Input will contain no duplicate strings.

### Output

• Line 1: A single line containing K, the number of strings that could be lexicographically first.

• Lines 2..1+K: The (1+i)th line should contain the ith string that could be lexicographically first. Strings should be output in the same order they were given in the input.

#### Sample Input

4

omm

moo

mom

ommnom

INPUT DETAILS: The example from the problem statement.

#### Sample Output

2

omm

mom

OUTPUT DETAILS: Only “omm” and “mom” can be ordered first.

### 题解

#include <cstdio>
#include <queue>
#include <cstring>
#define N 300010
using namespace std;
struct edge
{
int to,next;
}e[1010];
int n,tot,cnt,ans,sums;
int ch[N][26],is_end[N],can[30010],st[30],len[30010],d[30],root,sz;
queue<int> q;
char s[30010][50];
{
e[++tot].next=st[x];
e[tot].to=y,st[x]=tot;
//  printf("%d %d\n",x,y);
}
queue<int>que;
main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
scanf("%s",s[i]+1);
int len=strlen(s[i]+1),now=root;
for (int j=1;j<=len;j++)
if (ch[now][s[i][j]-'a'])
now=ch[now][s[i][j]-'a'];
else
now=ch[now][s[i][j]-'a']=++sz;
if (!is_end[now])
is_end[now]=i,can[i]=1;
}
for (int i=1;i<=n;i++)
if (can[i])
{
int len=strlen(s[i]+1),now=root;
for (int j=1;j<=len;now=ch[now][s[i][j]-'a'],j++)
if (is_end[now])
can[i]=0;
if (!can[i]) continue;
memset(st,-1,sizeof st);
memset(e,0,sizeof e);
memset(d,0,sizeof d);
tot=0,now=root,sums=0;
for (int j=1;j<=len;now=ch[now][s[i][j]-'a'],j++)
for (int k=0;k<26;k++)
if (k!=s[i][j]-'a' && ch[now][k])
for (int j=0;j<26;j++)
if (!d[j])
que.push(j);
while(!que.empty())
{
int now=que.front();que.pop();
sums++;
for (int j=st[now];~j;j=e[j].next)
{
d[e[j].to]--;
if (!d[e[j].to]) que.push(e[j].to);
}
}
if (sums<26)
can[i]=0;
else
ans++;
}
printf("%d\n",ans);
for (int i=1;i<=n;i++)
if (can[i])
printf("%s\n",s[i]+1);
}