# BZOJ 3012: [Usaco2012 Dec]First! 字典树 拓扑排序

## 3012: [Usaco2012 Dec]First!

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 216  Solved: 101
[Submit][Status][Discuss]

## 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<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>

using namespace std;

typedef long long ll;

{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=300100;

int tot,danger[N],tr[N],fal[N/10],sz;
char s[N/10];
bool book[N],mp;

inline void insert(int x)
{
int len=strlen(s[x]+1);
register int i,k,now=0;
for(i=1;i<=len;++i)
{
k=s[x][i]-'a';
if(!tr[now][k])tr[now][k]=++sz;
now=tr[now][k];if(danger[now])return ;
}
danger[now]=1;book[x]=1;
}

int ecnt,last,ind;
struct EDGE{int to,nt;}e[N];
{e[++ecnt]=(EDGE){v,last[u]};last[u]=ecnt;ind[v]++;}

int q;
inline bool topo()
{
for(i=0;i<26;++i)if(0==ind[i])q[tail++]=i;
{
for(i=last[u];i;i=e[i].nt)
{ind[e[i].to]--;if(!ind[e[i].to])q[tail++]=e[i].to;}
}
return tail==26;
}

inline bool check(int x)
{
int len=strlen(s[x]+1);
register int now=0,i,j,k;
for(i=1;i<=len;++i)
{
k=s[x][i]-'a';if(danger[now])return 0;
now=tr[now][k];
}
}

inline void initial()
{ecnt=0;memset(last,0,sizeof(last));memset(ind,0,sizeof(ind));}

int main()
{
for(i=1;i<=n;++i)
{scanf("%s",s[i]+1);insert(i);}
for(i=1;i<=n;i++)
{
if(!book[i])continue;
initial();
if(!check(i))continue;
fal[++ans]=i;
}
print(ans);puts("");
for(i=1;i<=ans;++i)puts(s[fal[i]]+1);
return 0;
}
/*
4
omm
moo
mom
ommnom

2
omm
mom
*/ 