【[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有很多语言的 ...
随机推荐
- [转]象棋AI算法(二)
本文转自:http://blog.csdn.net/u012723995/article/details/47143569 参考文献:http://bbs.blueidea.com/thread-30 ...
- android socket 通讯(客户端) 发送数据
/** ClientSocket通讯类 **/ public class ClientSocket { /**服务器地址*/ private String serverUrl=&q ...
- dns-prefetch使用整理
网站投放百度.谷歌联盟广告,百度分享.推荐等,由于不同的DNS请求,会增加了网页加载时间,用户等待时间过长会造成跳出率增高,对SEO有一定影响. DNS解析速度是造成页面延迟加载的最大的原因. DNS ...
- Hibernate 学习(一)
一.Hibernate 简介 1.Hibernate 简介 Hibernate是一个开放源代码的对象关系映射(ORM)框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系, ...
- DataTables添加额外的查询参数和删除columns等无用参数
//1.定义全局变量 var iStart = 0, searchParams={}; //2.配置datatable的ajax配置项 "ajax": { "url&qu ...
- HDU 4283 (第k个出场 区间DP)
http://blog.csdn.net/acm_cxlove/article/details/7964594 http://www.tuicool.com/articles/jyaQ7n http: ...
- AngularJS模块之$scope
Angular中创建一个模块: angular.module("myApp",[]). controller("myController",function(& ...
- JVM jmap
需求:经常会因为OOM而导致系统挂掉,很多服务无法连接,所以准备了解一下. 参考:http://www.open-open.com/lib/view/open1390916852007.html 一. ...
- JavaScript中的appendChild()方法
appendChild()方法是向节点添加最后一个子节点.也可以使用此方法从一个元素向另一个元素移动元素. 案例一:向节点添加最后一个子节点 <!DOCTYPE html> <htm ...
- linux crontab 的使用
linux crontab 的使用 准备(实验楼需要,实际环境不需要):sudo service rsyslog startsudo cron -f & crontab 使用添加任务:cron ...