题目大意:

给定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. windows常用dos命令

    常用命令: d: 回车   磁盘切换 dir: 查看该目录下所有的文件和文件夹: md: 创建文件加 rd: 删除目录 cd: 进入指定的目录 cd..:回退到上级目录 cd\  :回退到根目录 de ...

  2. Unity3d NGUI 360度旋转

    [AddComponentMenu("NGUI/Examples/Spin With Mouse")] publicclass SpinWithMouse : MonoBehavi ...

  3. extendgcd模板

    看了数论第一章,终于搞懂了扩展欧几里德,其实就是普通欧几里德的逆推过程. // ax+by = gcd(a,b) ->求解x,y 其中a,b不全为0,可以为负数// 复杂度:O(log2a)vo ...

  4. 第三课 nodejs读取文件

    //引入文件操作模块var fs = require('fs'); //读取文件 使用 回调函数 utf-8编码读取 a.txt在当前文件目录fs.readFile('a.txt','UTF-8',f ...

  5. centos7 运行postgres 数据库脚本db.sql

    [root@localhost ~]# su postgresbash-4.2$ psqlcould not change directory to "/root": Permis ...

  6. bug-5——(js)indexOf()

    indexOf()方法可返回某个指定的字符串值在字符串中首次出现的位置. ①对大小写敏感 ②如果要检索的字符串值没有出现,则该方法返回-1. ③位置时从0开始的 $j(this).html().ind ...

  7. shell编程2 ---条件判断语句

    shell编程2 一.条件判断语句 1.按照文件类型进行判断 eg:test -e /etc/passwd  或者  [ -e /etc/passwd ]  &&echo yes || ...

  8. 33_为应用添加多个Activity与参数传递

    1\ 2\ 3\ 4\ 2 3

  9. mysql存储引擎(待补充)

    数据库中的表也应该有不同的类型,表的类型不同,会对应mysql不同的存取机制,表类型又称为存储引擎 存储引擎说白了就是如何存取数据.如何为存储的数据建立索引和如何更新.查询数据等技术的实现方法.因为在 ...

  10. static_new

    <?php //在::操作符的左边写上类的名称来静态地访问某个成员,这样就可以避免创建类的实例. //这样不仅可以省略掉实例化类的代码,而且还会更高效,因为类的每个实例都会占用一小部分的系统资源 ...