【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\n",ans);
for(i=1;i<=n;i++) if(can[i]) printf("%s\n",str[i]);
return 0;
}

【BZOJ3012】[Usaco2012 Dec]First! Trie树+拓补排序的更多相关文章

  1. BZOJ_3012_[Usaco2012 Dec]First!_trie树+拓扑排序

    BZOJ_3012_[Usaco2012 Dec]First!_trie树+拓扑排序 题意: 给定n个总长不超过m的互不相同的字符串,现在你可以任意指定字符之间的大小关系.问有多少个串可能成为字典序最 ...

  2. 【BZOJ3036】绿豆蛙的归宿 拓补排序+概率

    [BZOJ3036]绿豆蛙的归宿 Description 随着新版百度空间的下线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 给出一个有向无环的连通图,起点为1终点为N,每条边都有一个长度. ...

  3. [USACO11JAN]Roads and Planes G【缩点+Dij+拓补排序】

    题目 Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条道路 (1 < ...

  4. BZOJ 3012: [Usaco2012 Dec]First! 字典树 + tarjan

    Code: #include<bits/stdc++.h> #define maxn 1000003 using namespace std; char str[maxn],strtot[ ...

  5. BZOJ3012 : [Usaco2012 Dec]First!

    建立Trie,那么成为答案的串必须满足其终止节点到根路径上没有其它点. 对于Trie上每个节点维护一个bitset,表示哪些字符必须在哪些字符之前. 每到达一个可能成为答案的终止节点,对图进行拓扑排序 ...

  6. bzoj 3012: [Usaco2012 Dec]First! Trie+拓扑排序

    题目大意: 给定n个总长不超过m的互不相同的字符串,现在你可以任意指定字符之间的大小关系.问有多少个串可能成为字典序最小的串,并输出这些串.n <= 30,000 , m <= 300,0 ...

  7. HDU 1811 Rank of Tetris 拓补排序+并查集

    Rank of Tetris Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [ ...

  8. poj 3687 Labeling Balls(拓补排序)

    Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them ...

  9. poj 1094 Sorting It All Out 拓补排序

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

随机推荐

  1. iOS彩票项目--第六天,运用MVC思想搭建设置界面(非storyboard方法)

    一.我只想说封装的思想很重要,MVC的思想也很重要,利用MVC思想搭建一下的界面 先说显示出来的cell,有三种(图中的两种,还有一种是最普通的,没有图片的),这种显示不同的cell,交给模型来处理, ...

  2. R语言进阶之4:数据整形(reshape)

    一.通过重新构建数据进行整形 数据整形最直接的思路就把数据全部向量化,然后按要求用向量构建其他类型的数据.这样是不是会产生大量的中间变量.占用大量内存?没错.R语言的任何函数(包括赋值)操作都会有同样 ...

  3. 受打击了:你是学.net 的吧?

    我在网上投了简历,今天去面试, 去到才知道有面试题做,做完之后自知答的很烂. 没想到面试我的那个人,一开始就很直接,说: 我感觉你很喜欢用英语, 但英语很烂 我觉得你很喜欢用别人的东西, 但技术水平很 ...

  4. ADC相关参数之---分辨率和精度

    ADC的分辨率被定义为输入信号值的最小变化,这个最小数值变化会改变数字输出值的一个数值.对于一个理想ADC来说,传递函数是一个步宽等于分辨率的阶梯.然而,在具有较高分辨率的系统中(≥16位),传输函数 ...

  5. Extracting and composing robust features with denosing autoencoders 论文

    这是一篇发表于2008年初的论文. 文章主要讲了利用 denosing autoencoder来学习 robust的中间特征..进上步,说明,利用这个方法,可以初始化神经网络的权值..这就相当于一种非 ...

  6. Onject.Instantiate实例

    该函数有两个函数原型: Object Instantiate(Object original,Vector3 position,Quaternion rotation); Onject Instant ...

  7. u3d调用自己的dll

    原文地址:http://blog.sina.com.cn/s/blog_62f7cb730100zhhf.html 首先用vc建立一个dll工程 然后在里面建立一个testunity.h文件.内容如下 ...

  8. TMS320F28335项目开发记录3_28335简介

    28335特性介绍 高性能静态CMOS技术         高达150MHZ(6.67ns的周期时间):1.9V / 1.8内核 ,3.3V I/O设计 高性能32位CPU         IEEE- ...

  9. 如何使用ChemDraw改变说明文本

    作为一款全球领先的化学绘图工具,ChemDraw能够绘制各种复杂的结构方程式.ChemDraw软件还增加了新的绘图工具,能够方便化学领域的图形绘制.本教程将向大家讲解如何在ChemDraw中改变说明文 ...

  10. 在程序中使用命令行的方式来调用py文件

    做这个主要是程序可以做到直接调用一个脚本,而不是从脚本中把类或者函数import出来这样调用,比如我们写的python命令行文件,让java来调用,让c++来调用,都是可以的.这样不需要整个语言都用p ...