zoukankan      html  css  js  c++  java
  • [USACO12DEC]第一!First!(字典树,拓扑排序)

    [USACO12DEC]第一!First!

    题目描述

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

    Bessie一直在研究字符串。她发现,通过改变字母表的顺序,她可以按改变后的字母表来排列字符串(字典序大小排列)。

    例如,Bessie发现,对于字符串串“omm”,“moo”,“mom”和“ommnom”,她可以使用标准字母表使“mom”排在第一个(即字典序最小),她也可以使用字母表“abcdefghijklonmpqrstuvwxyz”使得“omm”排在第一个。然而,Bessie想不出任何方法(改变字母表顺序)使得“moo”或“ommnom”排在第一个。

    接下来让我们通过重新排列字母表的顺序来计算输入中有哪些字符串可以排在第一个(即字典序最小),从而帮助Bessie。

    要计算字符串X和字符串Y按照重新排列过的字母表顺序来排列的顺序,先找到它们第一个不同的字母X[i]与Y[i],按重排后的字母表顺序比较,若X[i]比Y[i]先,则X的字典序比Y小,即X排在Y前;若没有不同的字母,则比较X与Y长度,若X比Y短,则X的字典序比Y小,即X排在Y前。

    输入输出格式

    输入格式:

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

    第1行:一个数字N(1 <= N <= 30,000),Bessie正在研究的字符串的数量。

    第2~N+1行:每行包含一个非空字符串。所有字符串包含的字符总数不会超过300,000。 输入中的所有字符都是小写字母,即a~z。 输入不包含重复的字符串。

    输出格式:

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

    第1行:一个数字K,表示按重排后的字母表顺序排列的字符串有多少可以排在第一个数量。

    第2~K+1行:第i+1行包含第i个按重排后的字母表顺序排列后可以排在第一个的字符串。字符串应该按照它们在输入中的顺序来输出。

    输入输出样例

    输入样例#1:

    4
    omm
    moo
    mom
    ommnom

    输出样例#1:

    2
    omm
    mom

    说明

    The example from the problem statement.

    Only "omm" and "mom" can be ordered first.

    样例即是题目描述中的例子,只有“omm”和“mom”在各自特定的字典序下可以被排列在第一个。

    题解

    刚学字典树的时候看到这道题,感觉用广搜每次把其他的连边判一下(我是真的蠢,这样想还把时间复杂度算错了),没想到字典树里同一层中没有相同的节点,直接连边即可。最后用拓扑排序判一下环即可。代码略丑

    #include<bits/stdc++.h>
    using namespace std;
    int n,cnt,ans,trie[300010][27],len[30010];
    int head[30],du[30],qwe[30010];bool vis[30][30],in[30],end[300010];
    struct node{
    	int to,next;
    }edge[900];
    string s[30010];
    void add(int x,int y)
    {
    	cnt++;edge[cnt].to=y;edge[cnt].next=head[x];head[x]=cnt;
    }
    queue<int> q;
    bool topu()
    {
    	for(int i=1;i<=26;i++)
    	{
    		if(!du[i]) q.push(i),in[i]=1;
    	}
    	while(q.size())
    	{
    		int u=q.front();q.pop();
    		for(int i=head[u];i;i=edge[i].next)
    		{
    			int v=edge[i].to;if(in[v])continue;
    			du[v]--;if(!du[v])q.push(v),in[v]=1;
    		}
    	}
    	for(int i=1;i<=26;i++)if(!in[i])return false;return true;
    }
    void clear()
    {
    	cnt=0;memset(head,0,sizeof(head));memset(in,0,sizeof(in));memset(du,0,sizeof(du));
    	memset(vis,0,sizeof(vis));
    }
    int main()
    {
    	scanf("%d",&n);
    	for(int i=1;i<=n;i++)
    	{
    		cin>>s[i];
    		len[i]=s[i].length();int now=0;
    		for(int j=0;j<len[i];j++)
    		{
    			if(!trie[now][s[i][j]-'a'+1]) trie[now][s[i][j]-'a'+1]=++cnt;
    			now=trie[now][s[i][j]-'a'+1];
    		}
    		end[now]=1;
    	}
    	for(int i=1;i<=n;i++)
    	{
    		int now=0;clear();bool flag=0;
    		for(int j=0;j<len[i];j++)
    		{
    			int d=s[i][j]-'a'+1;
    			for(int k=1;k<=26;k++)
    			{
    				if(k==d)continue;
    				if(trie[now][k]&&vis[k][d]){flag=1;break;}
    				if(trie[now][k]&&!vis[d][k])
    				{
    					add(d,k);du[k]++;vis[d][k]=1;
    				}
    			}
    			if(end[now]||flag){flag=1;break;}now=trie[now][d];
    		}
    		if(flag)continue;
    		if(topu()) ans++,qwe[ans]=i;
    	}
    	cout<<ans<<endl;
    	for(int i=1;i<=ans;i++)cout<<s[qwe[i]]<<endl;
    
    }
    
  • 相关阅读:
    Attach Files to Objects 将文件附加到对象
    Provide Several View Variants for End-Users 为最终用户提供多个视图变体
    Audit Object Changes 审核对象更改
    Toggle the WinForms Ribbon Interface 切换 WinForms 功能区界面
    Change Style of Navigation Items 更改导航项的样式
    Apply Grouping to List View Data 将分组应用于列表视图数据
    Choose the WinForms UI Type 选择 WinForms UI 类型
    Filter List Views 筛选器列表视图
    Make a List View Editable 使列表视图可编辑
    Add a Preview to a List View将预览添加到列表视图
  • 原文地址:https://www.cnblogs.com/lsgjcya/p/9477714.html
Copyright © 2011-2022 走看看