zoukankan      html  css  js  c++  java
  • 【BZOJ3012】[Usaco2012 Dec]First! Trie树+拓补排序

    【BZOJ3012】[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].

    给定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.

    题解:先将单词都塞到Trie树里,然后考虑如果想让一个单词排在最前面,首先它肯定不能包含其他单词(这点卡了我很久)

    然后我们在Trie树里遍历这个单词对应的链,如果想让这个链排在最前面,只需要让链上每个节点的排序都比它的兄弟靠前(同父的兄弟),最后处理出所有大小关系,跑一个拓补排序判环就行了

    我到官网查了数据,发现所有单词的长度都不超过20,所以不用担心空间的问题

     

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <queue>
    using namespace std;
    const int maxn=300010;
    int n,tot,cnt,ans,sum;
    int ch[maxn][26],num[maxn],can[30010],head[30],next[1000],to[1000],len[30010],d[30];
    queue<int> q;
    char str[30010][50];
    void add(int a,int b)
    {
    	to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;
    }
    int main()
    {
    	scanf("%d",&n);
    	int i,j,k,u;
    	for(i=1;i<=n;i++)
    	{
    		scanf("%s",str[i]);
    		len[i]=strlen(str[i]);
    		u=0;
    		for(j=0;j<len[i];j++)
    		{
    			if(!ch[u][str[i][j]-'a'])	ch[u][str[i][j]-'a']=++tot;
    			u=ch[u][str[i][j]-'a'];
    		}
    		if(!num[u])	num[u]=i,can[i]=1;
    	}
    	memset(head,-1,sizeof(head));
    	for(i=1;i<=n;i++)
    	{
    		if(!can[i])	continue;
    		memset(head,-1,sizeof(head)),memset(d,0,sizeof(d));
    		sum=cnt=u=0;
    		for(j=0;j<len[i];j++)
    		{
    			if(num[u])
    			{
    				can[i]=0;
    				break;
    			}
    			for(k=0;k<26;k++)	if(k!=str[i][j]-'a'&&ch[u][k])	d[k]++,add(str[i][j]-'a',k);
    			u=ch[u][str[i][j]-'a'];
    		}
    		if(!can[i])	continue;
    		for(j=0;j<26;j++)	if(!d[j])	q.push(j);
    		while(!q.empty())
    		{
    			u=q.front(),q.pop(),sum++;
    			for(j=head[u];j!=-1;j=next[j])
    			{
    				d[to[j]]--;
    				if(!d[to[j]])	q.push(to[j]);
    			}
    		}
    		if(sum<26)	can[i]=0;
    		else	ans++;
    	}
    	printf("%d
    ",ans);
    	for(i=1;i<=n;i++)	if(can[i])	printf("%s
    ",str[i]);
    	return 0;
    }

     

  • 相关阅读:
    ABAP Webdynpro Interface View的用法
    ABAP Webdynpro的跟踪工具WD_TRACE_TOOL
    git 速查
    Python 解析含有命名空间(xmlns)的xml文件(基于ElementTree)
    完全显示DataFrame中行、列内容
    解决Jupyter Notebook中for循环输出DataFrame不够美观
    git配置别名
    元素可拖拽(移动端与pc端)
    pointer network和recursive神经网络
    ELMO,BERT和GPT简介
  • 原文地址:https://www.cnblogs.com/CQzhangyu/p/6639185.html
Copyright © 2011-2022 走看看