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

  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. svn 不能校验路径“XXX”的锁;没有匹配的可用锁令牌 故障解决方法

    原文出自:https://blog.csdn.net/seesun2012 故障现象: svn Commit ,失败,提示: 不能校验路径"/SEESUN/****系统计划说明书.docx& ...

  2. 使用union合并查询

    语法: select …..from 表1 union select ……from 表2 2. 合并查询的特点 ① 合并的表中的列的个数.数据类型必须相同或向兼容. ② union默认去掉重复值,如果 ...

  3. 基于Java实现简单亚马逊爬虫

    前言:最近博主买了台Kindle,感觉亚马逊上的图书资源质量挺好,还时不时地会有价格低但质量高的书出售,但限于亚马逊并没有很好的优惠提醒功能,自己天天盯着又很累.于是,我自己写了一个基于Java的亚马 ...

  4. js控制input text字符键入/字符长度限制/字母自动大写

    功能: 1.仅允许指定字符键入 2.限制长度 实现代码: <input type="text" style="width: 6em" name=" ...

  5. unity3d之技能栏冷却

    绑定在按钮上的脚本 using UnityEngine; using System.Collections; using UnityEngine.UI; public class CdCover : ...

  6. 08_Spring自定义标签

    [ 项目工程 ] [ Person.java 模型类 ] package com.spring.selfxml.model; /** * Created by HigginCui on 2018/9/ ...

  7. 11_Redis集群

    [Redis集群分类] 1.主从复制(master/slave) 2.高可用Sentinel哨兵 3.高可用集群模式 [ 主从复制(master/slave)] [Redis一主多从架构] 通过持久化 ...

  8. Android DB类,支持MDB,SQLITE,SQLSERVER,支持查询、事务,对象直接插入和更新操作等

    直做数据库,最近花了点时间把自己常用的东西封装在一起. DBHelper using System; using System.Collections.Generic; using System.Te ...

  9. 第9课 备忘便签-TinyDB

    编写一只个性化的App便签小程序,TinyDB可能会帮上你.   1.组件设计 1)TextBox1输入需要记录存储的信息记录 2)四只button分别作为“添加 编辑 删除清空”信息记录 3)Lli ...

  10. Android Animation 知识点速记备忘思维导图

    备注的大段文本,无法在图片中体现, 思维导图源文件放在附件中.使用 Xmind 8 制作. 附件:AndroidAnimation-xmind.zip