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

版权声明:想转就转吧,注明出处就行 括弧笑 https://blog.csdn.net/BlackJack_/article/details/74783615

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].

给定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
INPUT DETAILS: The example from the problem statement.

Sample Output

2
omm
mom

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

字典树建出来

对于两个串

如果其中一个是另一个的前缀 那么前缀的rank一定要比另一个高 直接跳出


对于一个串的某一位置

如果它优先级最高 那显然

该节点的优先级高于它的所有兄弟

按照这种方式连边

如果有环 则不成立


拓扑排序判环:如果搞完之后有点没进过队 有环

重置邻接表忘了ecnt调两个小时 醉了

#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;

inline int read()
{
	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][27],fal[N/10],sz;
char s[N/10][310];
bool book[N],mp[30][30];

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[30],ind[30];
struct EDGE{int to,nt;}e[N];
inline void add(int u,int v)
{e[++ecnt]=(EDGE){v,last[u]};last[u]=ecnt;ind[v]++;}

int q[30];
inline bool topo()
{
	register int head=0,tail=0,u,i;
	for(i=0;i<26;++i)if(0==ind[i])q[tail++]=i;
	while(head<tail)
	{
		u=q[head++];
		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;
		for(j=0;j<26;++j)if(j!=k&&tr[now][j])add(k,j);
		now=tr[now][k]; 
	}
	return topo();
}

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

int main()
{
	register int i,j,ans=0,n=read();
	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
*/


展开阅读全文

没有更多推荐了,返回首页