题目描述

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


其实题目意思还是很清晰易懂的。

一周之前就想写了这题,无奈一直考试(爆零无数)。

我们要通过修改字典序来使得某一个串的字典序最小。

我们考虑这样一个问题如果串$x$与其他的串的$l$前面的位置都一样,那么我们一定要让$l$位置的字符的字典序排在其他的前面。

这个明显的关系可以用拓扑排序。

所以对所有串建$trie$树,我们枚举一个串可以成为答案,然后把不匹配的字符连边,跑拓扑判断是否有环。


#include <bits/stdc++.h>
using namespace std;
#define reg register
int n, ans;
char str[][];
bool can[], cant[];
bool appear[][];
int nxt[][], tot, dep[], End[];
inline void Insert(char *s)
{
int len = strlen(s + );
int now = ;
for (reg int i = ; i <= len ; i ++)
{
if (!nxt[now][s[i]-'a']) nxt[now][s[i]-'a'] = ++tot, dep[nxt[now][s[i]-'a']] = dep[now] + ;
now = nxt[now][s[i]-'a'];
appear[dep[now]][s[i]-'a'] = ;
}
End[now] = ;
} void dfs(int x, bool ap)
{
if (ap) cant[x] = ;
for (reg int i = ; i <= ; i ++)
if (nxt[x][i]) dfs(nxt[x][i], ap | End[x]);
} vector <int> ve[];
int deg[];
bool ex[][]; inline bool Topsort()
{
queue <int> q;
for (reg int i = ; i <= ; i ++) if (!deg[i]) q.push(i);
while(!q.empty())
{
int x = q.front();q.pop();
for (reg int i = ; i < ve[x].size() ; i ++)
{
int to = ve[x][i];
deg[to]--;
if (!deg[to]) q.push(to);
}
}
for (reg int i = ; i <= ; i ++)
if (deg[i]) return ;
return ;
} inline bool Build(int id)
{
for (reg int i = ; i <= ; i ++) ve[i].clear(), deg[i] = ;
int len = strlen(str[id] + );
int now = ;
for (reg int i = ; i <= len ; i ++)
{
for (reg int j = 'a' ; j <= 'z' ; j ++)
{
if (!nxt[now][j-'a'] or j == str[id][i]) continue;
ve[j-'a'].push_back(str[id][i]-'a');
deg[str[id][i]-'a']++;
// printf("%c -> %c\n", j, str[id][i]);
}
now = nxt[now][str[id][i]-'a'];
if (cant[now]) return ;
}
return Topsort();
} int main()
{
scanf("%d", &n);
for (reg int i = ; i <= n ; i ++)
{
scanf("%s", str[i] + );
Insert(str[i]);
}
dfs(, );
for (reg int i = ; i <= n ; i ++)
{
can[i] = Build(i);
ans += can[i];
// printf("%d %d\n", i, can[i]);
}
cout << ans << endl;
for (reg int i = ; i <= n ; i ++)
if (can[i]) {
int len = strlen(str[i] + );
for (reg int j = ; j <= len ; j ++) printf("%c", str[i][j]);
puts("");
}
return ;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

  7. P3065 [USACO12DEC]第一!First!

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

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

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

  9. 菜鸟Python学习笔记第一天:关于一些函数库的使用

    2017年1月3日 星期二 大一学习一门新的计算机语言真的很难,有时候连函数拼写出错查错都能查半天,没办法,谁让我英语太渣. 关于计算机语言的学习我想还是从C语言学习开始为好,Python有很多语言的 ...

随机推荐

  1. 【学习笔记】第三章 python3核心技术与实践--Jupyter Notebook

    可能你已经知道,Python 在 14 年后的“崛起”,得益于机器学习和数学统计应用的兴起.那为什么 Python 如此适合数学统计和机器学习呢?作为“老司机”的我可以肯定地告诉你,Jupyter N ...

  2. 第二次实验报告:使用Packet Tracer分析应用层协议

    个人信息:      •  姓名:李微微       •  班级:计算1811       •  学号:201821121001 一.摘要 本文描述使用Packet Tracer,正确配置网络参数,抓 ...

  3. Rocksdb基本用法

    rocksdb 用法 rocksdb 介绍 RocksDB是使用C++编写的嵌入式kv存储引擎,其键值均允许使用二进制流.由Facebook基于levelDB开发, 提供向后兼容的levelDB AP ...

  4. Day 18 软件管理3之搭建网络仓库

    搭建一个网络仓库 服务端: 10.0.0.200   1.准备软件包( 1.光盘 2.缓存 3.联网下载 4.同步 ) 2.通过p共享软件包存放的目录 3.将光盘中的软件包都拷贝至p的共享目录下 4. ...

  5. js中Math对象常用的属性和方法

    1 Math对象 1.1定义:Math是js的一个内置对象,它提供了一些数学方法. 1.2特性:不能用构造函数的方式创建,无法初始化,只有静态属性和方法 1.3静态属性 1.3.1 Math.PI 圆 ...

  6. 《JavaScript DOM 编程艺术》 学习笔记

    目录 <JavaScript DOM 编程艺术> 学习笔记 第一章 js简史 第二章 js语法 准备工作 语法 第三章 DOM DOM中的D DOM中的O DOM中的M 第四章 js图片库 ...

  7. [Flask Tips]Flask-APScheduler用法总结

    在应用中需要使用调度框架来做一些统计的功能,可惜在Windows上可用的不多,最后选择了APScheduler这个调度器. 用法不多介绍,只总结一下在使用中遇到的坑. app_context 问题 凡 ...

  8. .net core 发布单个exe 文件, 并优化缩小大小

    最新版的.net core 3.0 可以通过命令行发布为exe文件, 操作步骤如下: 在项目目录下打开控制台: 输入命令: dotnet publish -r win-x64 -c Release - ...

  9. [python]兔子问题,斐波那契数列 递归&非递归

    假设一对幼年兔子需要一个月长成成年兔子,一对成年兔子一个月后每个月都可以繁衍出一对新的幼年兔子(即兔子诞生两个月后开始繁殖).不考虑死亡的情况,问第 N 个月时共有多少对兔子? 结果前几个月的兔子数量 ...

  10. SpringCloudEureka入门

    说明 SpringBoot版本 2.1.7.RELEASE SpringCloud版本 Greenwich.SR2 创建eureka server工程 加入pom依赖 <dependencies ...