题目描述

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”在各自特定的字典序下可以被排列在第一个。

Solution:

  本题trie树+拓扑排序。

  首先,我们不难想到若一个字符串是另一个字符串的前缀,则后者一定不可能排在第一个,那么这种情况直接可以trie树判掉,然后我们可以枚举每个字符串作为第一个串,在trie树中查询该串时对于与当前指针同层次的指针建字符连向字符的有向边并统计入度(表示当前指针所带表字符要比同层次的其它指针表示的字符字典序小),然后就能得到一张图,只要该图无环就说明该字符串可行,需要判环于是想到在该图上跑拓扑排序,若能使每个字符的入度均为0说明可行,否则不行。

代码:

#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=,M=;
int trie[M][],tot=;
int n,ans,to[M],net[M],rd[N],h[N],cnt;
char s[][];
bool end[M],ct[M]; il void cler(){cnt=;memset(h,,sizeof(h));memset(rd,,sizeof(rd));} il void add(int u,int v){to[++cnt]=v,net[cnt]=h[u],h[u]=cnt,rd[v]++;} il bool bfs(){
queue<int>q;
For(i,,) if(!rd[i]) q.push(i);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=h[u];i;i=net[i])
if(--rd[to[i]]==)q.push(to[i]);
}
For(i,,) if(rd[i])return ;
return ;
} il void insert(char *s){
int len=strlen(s),p=,x;
For(i,,len-){
x=s[i]-'a';
if(!trie[p][x])trie[p][x]=++tot;
p=trie[p][x];
}
end[p]=;
} il bool search(char *s){
cler();
int len=strlen(s),p=,x;
For(i,,len-){
x=s[i]-'a';
if(end[p])return ;
For(j,,) if(x!=j&&trie[p][j]) add(x+,j+);
p=trie[p][x];
}
return ;
} int main(){
scanf("%d",&n);
For(i,,n) scanf("%s",s[i]),insert(s[i]);
For(i,,n) if(search(s[i])&&bfs()) ct[i]=,ans++;
printf("%d\n",ans);
For(i,,n) if(ct[i]) puts(s[i]);
return ;
}

P3065 [USACO12DEC]第一!First!的更多相关文章

  1. [luogu P3065] [USACO12DEC]第一!First!

    [luogu P3065] [USACO12DEC]第一!First! 题目描述 Bessie has been playing with strings again. She found that ...

  2. 洛谷P3065 [USACO12DEC]第一!First!(Trie树+拓扑排序)

    P3065 [USACO12DEC]第一!First! 题目链接:https://www.luogu.org/problemnew/show/P3065 题目描述 Bessie一直在研究字符串.她发现 ...

  3. Luogu P3065 [USACO12DEC]第一!First!【字典树/拓扑排序】By cellur925

    题意:给你许多字符串,你可以改变字母序大小,问有哪些字符串可能成为字典序最小的字符串. 我们考虑把这些字符串都塞到\(trie\)树上.之后检索每一个字符串的时候,我们看和他同一层的地方是否有字符,如 ...

  4. [USACO12DEC]第一!First! (Trie树,拓扑排序)

    题目链接 Solution 感觉比较巧的题啊... 考虑几点: 可以交换无数次字母表,即字母表可以为任意形态. 对于以其他字符串为前缀的字符串,我们可以直接舍去. 因为此时它所包含的前缀的字典序绝对比 ...

  5. [USACO12DEC]第一!First!(字典树,拓扑排序)

    [USACO12DEC]第一!First! 题目描述 Bessie has been playing with strings again. She found that by changing th ...

  6. [bzoj3012][luogu3065][USACO12DEC][第一!First!] (trie+拓扑排序判环)

    题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...

  7. 【[USACO12DEC]第一!First!】

    一个串不能成为第一的情况有两种 另外一个单词是它的前缀 在分配字母表大小关系的时候出现了矛盾的情况 第一种很好判断,一旦我们在一个单词没有匹配完之前遇到一个结束标志,那么就说明另外一个单词是它的前缀 ...

  8. [Luogu3065][USACO12DEC]第一!First!

    题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...

  9. P3065 [USACO12DEC]First! G

    题意描述 [USACO12DEC]First! G 不错的一道题. 给你 \(N\) 个字符串,要求你求出可能的字典序最小的字符串. 对于 可能的最小的字符串,你可以任意排列 \(26\) 个字母,使 ...

随机推荐

  1. VR中为什么需要把游戏音频放在聚光灯里?

    VR中为什么需要把游戏音频放在聚光灯里? 本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/de ...

  2. Luogu P3120 [USACO15FEB]牛跳房子(金)Cow Hopscotch (Gold)

    题目传送门 这是一道典型的记忆化搜索题. f[x][y]表示以x,y为右下角的方案数. code: #include <cstdio> #define mod 1000000007 usi ...

  3. 创龙DSP6748开发板LED闪烁-第一篇

    1. 首先看下DSP6748的GPIO寄存器的文档,先看下框图,有这个框图,一目了然,输入和输出很清楚 2. 看下寄存器部分,对应上面的图,问题在于,DSP6748有多少个GPIO?最多144个,下一 ...

  4. 「日常训练」Kefa and Park(Codeforces Round #321 Div. 2 C)

    题意与分析(CodeForces 580C) 给你一棵树,然后每个叶子节点会有一家餐馆:你讨厌猫(waht?怎么会有人讨厌猫),就不会走有连续超过m个节点有猫的路.然后问你最多去几家饭店. 这题我写的 ...

  5. MySQL日期函数、时间函数总结(MySQL 5.X)

    一.获得当前日期时间函数 1.1 获得当前日期+时间(date + time)函数:now() select now(); # :: 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下 ...

  6. Python 的非正式介绍

    在下面的例子中,通过提示符 (>>> 与 ...) 的出现与否来区分输入和输出:如果你想复现这些例子,当提示符出现后,你必须在提示符后键入例子中的每一个词:不以提示符开头的那些行是解 ...

  7. lintcode373 奇偶分割数组

    奇偶分割数组 分割一个整数数组,使得奇数在前偶数在后. 您在真实的面试中是否遇到过这个题? Yes 样例 给定 [1, 2, 3, 4],返回 [1, 3, 2, 4]. 我的方法:设定两个数组,分别 ...

  8. LeetCode 95——不同的二叉搜索树 II

    1. 题目 2. 解答 以 \(1, 2, \cdots, n\) 构建二叉搜索树,其中,任意数字都可以作为根节点来构建二叉搜索树.当我们将某一个数字作为根节点后,其左边数据将构建为左子树,右边数据将 ...

  9. SpringCloud IDEA 教学 (三) Eureka Client

    写在前头 本篇继续介绍基于Eureka的SpringCloud微服务搭建,回顾一下搭建过程, 第一步:建立一个服务注册中心: 第二步:建立微服务并注入到注册中心: 第三步:建立client端来访问微服 ...

  10. pxe+kickstart无人值守安装

    常用软件安装及使用目录 第1章 以前是怎么安装系统的 l 光盘(ISO文件,光盘的镜像文件)===>每一台物理机都得给一个光驱,如果用外置光驱的话,是不是每台机器都需要插一下 l U盘:ISO镜 ...