【[USACO12DEC]第一!First!】
一个串不能成为第一的情况有两种
另外一个单词是它的前缀
在分配字母表大小关系的时候出现了矛盾的情况
第一种很好判断,一旦我们在一个单词没有匹配完之前遇到一个结束标志,那么就说明另外一个单词是它的前缀
至于第二种,看到大小关系和是否矛盾我们很容易就联想到了拓扑排序
于是我们匹配的时候,发现某一层除了当前正在匹配的串以外还有其他字母,那么这些字母在我们构建的字母表中必须大于当前的这个字母,于是我们连一条有向边表示一下大小关系就好了
最后跑一遍拓扑判断一下是否有环就好了
#include<cstring>
#include<cstdio>
#include<iostream>
#include<string>
#define re register
#define maxn 300005
using namespace std;
struct node
{
int v,nxt;
}e[1005];
int son[maxn][26],flag[maxn];
std::string S[30005];
int len[30005];
int n,num,cnt,tot;
int head[26];
int ans[maxn];
inline void add_edge(int x,int y)
{
e[++num].v=y;
e[num].nxt=head[x];
head[x]=num;
}
inline void ins(int x)
{
int now=0;
for(re int i=0;i<len[x];i++)
{
if(!son[now][S[x][i]-'a']) son[now][S[x][i]-'a']=++cnt;
now=son[now][S[x][i]-'a'];
}
flag[now]=1;
}
inline int check(int x)
{
int now=0;
memset(e,0,sizeof(e));
int c[26];
memset(c,0,sizeof(c));
memset(head,0,sizeof(head));
num=0;
for(re int i=0;i<len[x];i++)
{
if(flag[now]) return 0;
for(re int j=0;j<26;j++)
if(son[now][j]&&S[x][i]-'a'!=j)
add_edge(S[x][i]-'a',j),c[j]++;
now=son[now][S[x][i]-'a'];
}
int cur=0;
int q[27];
memset(q,0,sizeof(q));
for(re int i=0;i<26;i++)
if(!c[i]) q[++cur]=i;
for(re int i=1;i<=cur;i++)
{
for(re int j=head[q[i]];j;j=e[j].nxt)
{
c[e[j].v]--;
if(!c[e[j].v]) q[++cur]=e[j].v;
}
}
return cur==26;
}
int main()
{
ios::sync_with_stdio(false);
cin>>n;
for(re int i=1;i<=n;i++)
{
cin>>S[i];
len[i]=S[i].size();
ins(i);
}
tot=0;
for(re int i=1;i<=n;i++)
if(check(i)) ans[++tot]=i;
cout<<tot<<endl;
for(re int i=1;i<=tot;i++)
cout<<S[ans[i]]<<endl;
return 0;
}
【[USACO12DEC]第一!First!】的更多相关文章
- [luogu P3065] [USACO12DEC]第一!First!
[luogu P3065] [USACO12DEC]第一!First! 题目描述 Bessie has been playing with strings again. She found that ...
- 洛谷P3065 [USACO12DEC]第一!First!(Trie树+拓扑排序)
P3065 [USACO12DEC]第一!First! 题目链接:https://www.luogu.org/problemnew/show/P3065 题目描述 Bessie一直在研究字符串.她发现 ...
- [USACO12DEC]第一!First! (Trie树,拓扑排序)
题目链接 Solution 感觉比较巧的题啊... 考虑几点: 可以交换无数次字母表,即字母表可以为任意形态. 对于以其他字符串为前缀的字符串,我们可以直接舍去. 因为此时它所包含的前缀的字典序绝对比 ...
- [USACO12DEC]第一!First!(字典树,拓扑排序)
[USACO12DEC]第一!First! 题目描述 Bessie has been playing with strings again. She found that by changing th ...
- [bzoj3012][luogu3065][USACO12DEC][第一!First!] (trie+拓扑排序判环)
题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...
- P3065 [USACO12DEC]第一!First!
题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...
- Luogu P3065 [USACO12DEC]第一!First!【字典树/拓扑排序】By cellur925
题意:给你许多字符串,你可以改变字母序大小,问有哪些字符串可能成为字典序最小的字符串. 我们考虑把这些字符串都塞到\(trie\)树上.之后检索每一个字符串的时候,我们看和他同一层的地方是否有字符,如 ...
- [Luogu3065][USACO12DEC]第一!First!
题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...
- 菜鸟Python学习笔记第一天:关于一些函数库的使用
2017年1月3日 星期二 大一学习一门新的计算机语言真的很难,有时候连函数拼写出错查错都能查半天,没办法,谁让我英语太渣. 关于计算机语言的学习我想还是从C语言学习开始为好,Python有很多语言的 ...
随机推荐
- [转]【Oracle Database 12c新特性】32k varchar2 max_string_size
本文转自:https://blogs.oracle.com/askmaclean/entry/oracle_database_12c%E6%96%B0%E7%89%B9%E6%80%A7_32k_va ...
- 2017年12月14日 LinQ高级查&&Asp.net WebForm Asp.net MVC
LinQ的高级查询用法 开头:StartsWith()结尾:EndsWith()模糊:Contains() 个数:Count最大值:Max(r => r.price)最小值:Min(r => ...
- Java Native Interface Specification Contents 翻译
https://docs.oracle.com/en/java/javase/12/docs/specs/jni/index.html Google翻译 第1章:简介 本章介绍Java Native ...
- window7下搭建本机版git并在本机上进行代码管理
1.安装软件:Git-1.9.2-preview20140411.exe与TortoiseGit-1.8.8.0-64bit.msi 2.在本机任一目录下执行如下操作: 3.创建一个test.java ...
- angular2-HttpClient
@angular/common/http中的HttpClient类,Angular 为应用程序提供了一个简化的 API 来实现 HTTP 功能.它基于浏览器提供的XMLHttpRequest接口. H ...
- Spring 框架(二)
1AOP 1.1 AOP介绍 1.1.1 什么是AOP l 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功 ...
- 老生常谈:++a与a--区别
a++的作用是先对a进行操作再++:a--则相反. 如: int a = 1;int b = a++; //此时先运算b=a,再a++,故b=1,a=2int c = --a; //此时先--a,再运 ...
- Android 初识Retrofit
什么是 Retrofit ? Retrofit 是一套 RESTful 架构的 Android(Java) 客户端实现,基于注解,提供 JSON to POJO(Plain Ordinary Java ...
- ANN神经网络——实现异或XOR (Python实现)
一.Introduction Perceptron can represent AND,OR,NOT 用初中的线性规划问题理解 异或的里程碑意义 想学的通透,先学历史! 据说在人工神经网络(artif ...
- win10 x64 python3.6 pycharm 安装statsmodels
在pycharm下,安装statsmodels,会出现需要vc++14.0的错误提示. 这时可以到网站 https://www.lfd.uci.edu/~gohlke/pythonlibs/#word ...