一个串不能成为第一的情况有两种

  1. 另外一个单词是它的前缀

  2. 在分配字母表大小关系的时候出现了矛盾的情况

第一种很好判断,一旦我们在一个单词没有匹配完之前遇到一个结束标志,那么就说明另外一个单词是它的前缀

至于第二种,看到大小关系和是否矛盾我们很容易就联想到了拓扑排序

于是我们匹配的时候,发现某一层除了当前正在匹配的串以外还有其他字母,那么这些字母在我们构建的字母表中必须大于当前的这个字母,于是我们连一条有向边表示一下大小关系就好了

最后跑一遍拓扑判断一下是否有环就好了

#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!】的更多相关文章

  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. [USACO12DEC]第一!First! (Trie树,拓扑排序)

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

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

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

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

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

  6. P3065 [USACO12DEC]第一!First!

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

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

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

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

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

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

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

随机推荐

  1. 跨平台 GUI可视化 网络调试工具

    mNetAssisthttp://blog.chinaunix.net/uid-21977056-id-4310527.htmlhttps://github.com/busyluo/mNetAssis ...

  2. 使用Electron开发桌面应用

    Electron 框架的前身是 Atom Shell,可以让你写使用 JavaScript,HTML 和 CSS 构建跨平台的桌面应用程序.它是基于io.js 和 Chromium 开源项目,并用于在 ...

  3. javascript预编译和执行过程总结

    javascript相对于其它语言来说是一种弱类型的语言,在其它如java语言中,程序的执行需要有编译的阶段,而在javascript中也有类似的“预编译阶段”(javascript的预编译是以代码块 ...

  4. easypoi导出单个sheet和多个sheet

    今天有时间研究了一下easypoi,感觉使用了easypoi导出excel方便了很多,不用写很多复杂的反射,只需要使用注解和一些工具类就可以实现常用的excel的导出,接下来介绍一下easypoi如何 ...

  5. 1、springboot之HelloWorld

    最基本的,官网copy 创建maven项目 maven中添加 <parent> <groupId>org.springframework.boot</groupId> ...

  6. poj 3260 最少硬币(01+多重+完全背包)

    http://www.cnblogs.com/ACMan/archive/2012/08/14/2637437.html #include <iostream> #include < ...

  7. PAT 1044. Shopping in Mars

    #include <cstdio> #include <cstdlib> #include <vector> #include <climits> #i ...

  8. thinkphp的删除操作

    1.循环遍历要删除的用户的或者呀删除的文章的id值: <volist name="list" id="vo"> <tr id="si ...

  9. Java xml 操作(Dom4J修改xml   + xPath技术  + SAX解析 + XML约束)

    1 XML基础 1)XML的作用 1.1 作为软件配置文件 1.2 作为小型的"数据库" 2)XML语法(由w3c组织规定的) 标签: 标签名不能以数字开头,中间不能有空格,区分大 ...

  10. Storm Flow

    A Stream represents the core data model in Trident, and can be thought of as a "stream" of ...