题目大意:

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

题解:

首先我们可以把所有的串插入到Trie树中。

然后我们枚举每个串,判断是否存在可行方案

我们枚举到一个串,那么我们就在Trie树中进行查找

每一次从某一个节点向下走的时候,我们都要保证当前走的这条支路是字典序最小的

也就是这条支路上的字母的字典序小于这个节点上的其他所有支路的字典序

所以我们就成功的找出了一些字典序的大小关系

所以我们只需要判断这个关系是不是存在环,如果存在环那么一定不可行

否则即可行

判环嘛。。。用拓扑排序不久好了嘛233.

然后让我来吐槽一下这个cin,cout会RE的题目

共30000个字符串,总长300000,是不是我要开一个30000*300000的char数组?

这内存不还得飞起来啊!!!!

用string不久好了? cin cout 不能用!!!你拿什么读入和输出啊!!!!

什么 cin cout 不能用???

可以用 ! 只是RE罢了!!!!

那怎么读入!!!??

你可以先读入一个char数组,然后转成string,然后输出的时候用putchar输出

。。。 。。。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 30010;
const int maxc = 300010;
int ch[maxc][27],nodecnt;
bool ed[maxc];
inline void insert(string s){
int nw = 0,len = s.size();
for(int i=0;i<len;++i){
if(ch[nw][s[i]-'a'] == 0) ch[nw][s[i]-'a'] = ++nodecnt;
nw = ch[nw][s[i]-'a'];
}ed[nw] = true;
}
bool mp[40][40];int deg[40];
int q[40],l,r;
inline bool judge(string s){
memset(mp,0,sizeof mp);
memset(deg,0,sizeof deg);
int nw = 0,len = s.size();
for(int i=0;i<len;++i){
for(int c='a';c<='z';++c){
if((c == s[i]) || (ch[nw][c - 'a'] == 0)) continue;
if(!mp[s[i]-'a'][c-'a']){
mp[s[i]-'a'][c-'a'] = true;
++deg[c-'a'];
}
}
if(ed[nw]) return false;
nw = ch[nw][s[i]-'a'];
}
l = 0;r = -1;
for(int i=0;i<26;++i){
if(deg[i] == 0) q[++r] = i;
}
while(l <= r){
int u = q[l++];
for(int i=0;i<26;++i){
if(mp[u][i]){
if(--deg[i] == 0) q[++r] = i;
}
}
}
if(r == 25) return true;
return false;
}
bool flag[maxn];
string s[maxn];
char str[maxc];
int main(){
int n;read(n);
for(int i=1;i<=n;++i){
scanf("%s",str);
int m = strlen(str);s[i] = "";
for(int j=0;j<m;++j){
s[i] = s[i] + str[j];
}insert(s[i]);
}int ans = 0;
for(int i=1;i<=n;++i){
if(judge(s[i])){
flag[i] = true;
++ ans;
}
}
printf("%d\n",ans);
for(int i=1;i<=n;++i){
if(flag[i]){
int m = s[i].size();
for(int j=0;j<m;++j){
putchar(s[i][j]);
}puts("");
}
}
getchar();getchar();
return 0;
}

bzoj 3012: [Usaco2012 Dec]First! Trie+拓扑排序的更多相关文章

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

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

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

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

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

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

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

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

  5. BZOJ 2938: [Poi2000]病毒 [AC自动机 拓扑排序]

    2938: [Poi2000]病毒 题意:判断是否存在无限长的不含模式串的字符串.只有01. 建出套路DP的转移图,判断有环就行了 练习一下拓扑排序 #include <iostream> ...

  6. E【中】假的字符串(trie+拓扑排序)

    题目 E[中]假的字符串 做法 一个字符串能作为最小值最基础的条件为不能出现前缀字符串 我们需要确定一种每个字符的排名使得\(s\)作为最小值,另有很多字符串\(t\),与\(s\)第一个不相同的位置 ...

  7. bzoj 4010: [HNOI2015]菜肴制作【拓扑排序】

    也就是给定有向图,求最小字典序的拓扑序,直接用小根堆就行(或者反着建图用大根堆) #include<iostream> #include<cstdio> #include< ...

  8. bzoj 3470: Freda’s Walk【拓扑排序+期望dp】

    dfs会T,只好正反两遍拓扑了-- #include<iostream> #include<cstdio> #include<queue> #include< ...

  9. bzoj 1774: [Usaco2009 Dec]Toll 过路费【排序+Floyd】

    非常迷的一道题啊 我觉得挺对的版本只得了30 总之就是Floyd·改,开两个数组,一个是d[i][j]就是普通的只有边权的最短路,a[i][j]是题目要求的那种 具体改的地方是把枚举中转点的地方把中转 ...

随机推荐

  1. Miller-Rabin大素数测试模板

    根据费马小定理: 对于素数n,a(0<a<n),a^(n-1)=1(mod n) 如果对于一个<n的正整数a,a^(n-1)!=1(mod n),则n必不是素数. 然后就可以随机生成 ...

  2. TRansportation ANalysis and SIMulation System

    https://www.fhwa.dot.gov/planning/tmip/transims/background.cfm?from=groupmessage

  3. 阿里云centos7搭建php+nginx环境

    阿里云Centos搭建lnmp(php7.1+nginx+mysql5.7) https://jingyan.baidu.com/article/215817f7a10bfb1eda14238b.ht ...

  4. Windows磁盘MBR结构详解

    在之前的文章 Windows存储管理之磁盘结构详解 中介绍了Windows的磁盘结构和MBR.本文将对Windows Basic Disk中的MBR的结构进行介绍,帮助读者更好的了解Windows系统 ...

  5. 3.30课·········Marquee标签

    页面的自动滚动效果,可由javascript来实现, 但是有一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用marquee ...

  6. shiro4----shiro3代码

    AuthenticationTest.java package cn.itcast.shiro.authentication; import org.apache.shiro.SecurityUtil ...

  7. 【leetcode刷题笔记】Reverse Nodes in k-Group

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  8. vue双向绑定补充说明方法

    本文总结自: https://segmentfault.com/a/1190000006599500,将每一个流程提炼出来做一个简单的说明,以免自己被繁杂的逻辑弄昏头脑~ observer: 遍历数据 ...

  9. Ansible playbook练习

    示例1:创建用户的Playbook --- - name: create user hosts: openstack gather_facts: false tasks: - name: create ...

  10. Spring Cloud之Swagger集群搭建

    在微服务中,Swagger是每个服务 比如会员服务,订单服务,支付服务 进行继承. 如何将整个微服务中的Swagger进行合成,同一台服务器上. 使用Zuul+Swagger实现管理整个微服务API文 ...