分析:

其实我们可以很容易的想到,如果一个串是另一个串的子串,那么必定长的那个串不可能是字典序最小的串。其次,如果一个串为了使他成为字典序最小的串儿出现了矛盾的情况,那么也不可能是字典序最小的串。那么,按照规则建出trie树,之后在枚举每个字符串,在枚举的同时建图,判断是否有环,用拓扑排序解决。

附上代码:

#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
#define N 30005
struct node
{
int son[27],flag;
}a[N*10];
struct no
{
int to,next;
}e[N];
int head[30],cnt,ans[N],in1[N],tot=1,n;
char s[N][305];
void add(int x,int y)
{
e[cnt].to=y;
e[cnt].next=head[x];
head[x]=cnt++;
in1[y]++;
}
void insert(int x)
{
int rt=1;
int len=strlen(s[x]);
for(int i=0;i<len;i++)
{
int idx=s[x][i]-'a'+1;
if(!a[rt].son[idx])a[rt].son[idx]=++tot;
rt=a[rt].son[idx];
}
a[rt].flag=1;
}
bool tsort()
{
queue <int>q;
for(int i=1;i<=26;i++)if(!in1[i])q.push(i);
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=head[x];i!=-1;i=e[i].next)
{
int to1=e[i].to;
in1[to1]--;
if(!in1[to1])q.push(to1);
}
}
for(int i=1;i<=26;i++)if(in1[i])return 0;
return 1;
}
bool check(int x)
{
int rt=1;
int len=strlen(s[x]);
for(int i=0;i<len;i++)
{
if(a[rt].flag)return 0;
int idx=s[x][i]-'a'+1;
for(int j=1;j<=26;j++)
{
if(j!=idx&&a[rt].son[j])add(idx,j);
}
rt=a[rt].son[idx];
}
return tsort();
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",&s[i]);
insert(i);
}
for(int i=1;i<=n;i++)
{
memset(head,-1,sizeof(head));
memset(in1,0,sizeof(in1));cnt=0;
if(!check(i))continue;
ans[++ans[0]]=i;
}
printf("%d\n",ans[0]);
for(int i=1;i<=ans[0];i++)
{
printf("%s\n",s[ans[i]]);
}
return 0;
}

  

[Usaco2012 Dec]First! BZOJ3012的更多相关文章

  1. 【BZOJ3012】[Usaco2012 Dec]First! Trie树+拓补排序

    [BZOJ3012][Usaco2012 Dec]First! Description Bessie has been playing with strings again. She found th ...

  2. BZOJ 3011: [Usaco2012 Dec]Running Away From the Barn( dfs序 + 主席树 )

    子树操作, dfs序即可.然后计算<=L就直接在可持久化线段树上查询 -------------------------------------------------------------- ...

  3. BZOJ_3012_[Usaco2012 Dec]First!_trie树+拓扑排序

    BZOJ_3012_[Usaco2012 Dec]First!_trie树+拓扑排序 题意: 给定n个总长不超过m的互不相同的字符串,现在你可以任意指定字符之间的大小关系.问有多少个串可能成为字典序最 ...

  4. BZOJ_3011_[Usaco2012 Dec]Running Away From the Barn _可并堆

    BZOJ_3011_[Usaco2012 Dec]Running Away From the Barn _可并堆 Description 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于l的 ...

  5. 【BZOJ3011】[Usaco2012 Dec]Running Away From the Barn 可并堆

    [BZOJ3011][Usaco2012 Dec]Running Away From the Barn Description It's milking time at Farmer John's f ...

  6. BZOJ3012 : [Usaco2012 Dec]First!

    建立Trie,那么成为答案的串必须满足其终止节点到根路径上没有其它点. 对于Trie上每个节点维护一个bitset,表示哪些字符必须在哪些字符之前. 每到达一个可能成为答案的终止节点,对图进行拓扑排序 ...

  7. [BZOJ3011][Usaco2012 Dec]Running Away From the Barn

    题意 给出一棵以1为根节点树,求每个节点的子树中到该节点距离<=l的节点的个数 题解 方法1:倍增+差分数组 首先可以很容易的转化问题,考虑每个节点对哪些节点有贡献 即每次对于一个节点,找到其第 ...

  8. bzoj 3012: [Usaco2012 Dec]First! Trie+拓扑排序

    题目大意: 给定n个总长不超过m的互不相同的字符串,现在你可以任意指定字符之间的大小关系.问有多少个串可能成为字典序最小的串,并输出这些串.n <= 30,000 , m <= 300,0 ...

  9. BZOJ 3012: [Usaco2012 Dec]First! 字典树 + tarjan

    Code: #include<bits/stdc++.h> #define maxn 1000003 using namespace std; char str[maxn],strtot[ ...

随机推荐

  1. SD从零开始13-14

    SD从零开始13 使用条件记录(Working with Condition Records) 定价报表—客户特定价格Pricing Reports-customer-specific prices ...

  2. MyBatis与JDBC连接数据库所使用的url之间的差异

    在Windows7 系统上安装了MySQL 8.0,然后创建Maven工程,配置pom.xml文件,添加了如下依赖: <dependency> <groupId>org.myb ...

  3. TensorFlow Saver 保存最佳模型 tf.train.Saver Save Best Model

      TensorFlow Saver 保存最佳模型 tf.train.Saver Save Best Model Checkmate is designed to be a simple drop-i ...

  4. servlet3.0 web.xml 标头

    <?xml version="1.1" encoding="UTF-8"?> <web-app xmlns="http://java ...

  5. Asp.Net MVC 模型(使用Entity Framework创建模型类)

    这篇教程的目的是解释在创建ASP.NET MVC应用程序时,如何使用Microsoft Entity Framework来创建数据访问类.这篇教程假设你事先对Microsoft Entity Fram ...

  6. python给邮箱发送消息

    首先要用到两个模块  并且大同你的发送邮箱smtp 最开始测试没打通了好久 smtplib是提供邮箱smtp服务, email是提供你发送消息的格式之类服务 import smtplib from e ...

  7. malloc,calloc,realloc函数用法,原理及不同解析

    https://blog.csdn.net/lixungogogo/article/details/50887028 一.malloc malloc在MSDN中原型为: void *malloc( s ...

  8. Django商城项目笔记No.9用户部分-注册接口签发JWTtoken

    Django商城项目笔记No.9用户部分-注册接口签发JWTtoken 我们在验证完用户的身份后(检验用户名和密码),需要向用户签发JWT,在需要用到用户身份信息的时候,还需核验用户的JWT. 关于签 ...

  9. 【干货】一文理解Druid原理架构(时序数据库,不是ali的数据库连接池)

    Druid.io(以下简称Druid)是2013年底开源出来的, 主要解决的是对实时数据以及较近时间的历史数据的多维查询提供高并发(多用户),低延时,高可靠性的问题. Druid简介: Druid是一 ...

  10. redis 持久化与备份策略

    持久化(persistence) 本文是 Redis 持久化文档 的中文翻译. 这篇文章提供了 Redis 持久化的技术性描述,推荐所有 Redis 用户阅读. 要更广泛地了解 Redis 持久化,以 ...