bzoj3012 题解
3012: [Usaco2012 Dec]First!
Desdescription
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].
给定n个总长不超过m的互不相同的字符串,现在你可以任意指定字符之间的大小关系。问有多少个串可能成为字典
序最小的串,并输出这些串。n <= 30,000 , m <= 300,000
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
Sample Output
2
omm
mom
王鉴浩论文题,对所有单词建Trie,然后dfs跑一遍该Trie。能成为最小的字典序的单词,有两个条件,第一个是对应的词尾节点应该在Trie树上的祖先中没有词尾节点。另外我们在跑其中一个单词的时候建一个图,图表示各个字母之间的大小关系。做法是每跑到一个节点,把其他同父子节点的字母向该节点的字母建一条边。然后我们跑到词尾节点的时候,dfs一遍建成的图看有没有环,没环说明能形成有效的字典序。这也是第二个条件:字典的大小关系图无环。
code:
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
using namespace std;
const int N=3e5+10;
const int M=3e4+10;
const int type=26;
struct node
{
int next[type];
int tag;
}trie[N];
int tot,n,m,ans;
char s[M][310];
int stot;
int infer[M];
int net[type+10][type+10];
void add(char *s,int odr)
{
int now=0;
int len=strlen(s),p;
for(int i=0;i<len;i++)
{
p=s[i]-'a';
if(!trie[now].next[p])
trie[now].next[p]=++tot;
now=trie[now].next[p];
}
trie[now].tag=odr;
return;
}
int vis[type+10];
void init()
{
tot=0;
clr(trie);
stot=0;
clr(infer);
clr(net);
return ;
}
void addedge(int u,int v)
{
net[u][v]++;
return ;
}
void deledge(int u,int v)
{
--net[u][v];
return ;
}
bool dfs(int u)
{
vis[u]=1;
int p;
for(int i=0;i<type;i++)
if(net[u][i])
{
if(vis[i]==1)
return true;
if(vis[i]==2)
continue;
if(dfs(i))
return true;
}
vis[u]=2;
return false;
}
void dfs(int now,int dep)
{
if(trie[now].tag)
{
int flag=0;
clr(vis);
for(int i=0;i<type;i++)
{
if(!vis[i] && dfs(i))
{
flag=1;
break;
}
}
if(!flag)
{
stot++;
infer[trie[now].tag]=1;
}
return ;
}
for(int i=0;i<type;i++)
if(trie[now].next[i])
{
for(int j=0;j<type;j++)
if(trie[now].next[j] && i!=j)
addedge(j,i);
dfs(trie[now].next[i],dep+1);
for(int j=type-1;j>=0;j--)
if(trie[now].next[j] && i!=j)
deledge(j,i);
}
return ;
}
int main()
{
init();
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",s[i]);
add(s[i],i);
}
stot=0;
dfs(0,0);
printf("%d
",stot);
for(int i=1;i<=n;i++)
if(infer[i])
printf("%s
",s[i]);
return 0;
}