bzoj3012 题解

3012: [Usaco2012 Dec]First!

Desdescription

Bessie has been playing with strings again. She found that by changing the order of the alphabet she could make some strings come before all the others lexicographically (dictionary ordering). For instance Bessie found that for the strings "omm", "moo", "mom", and "ommnom" she could make "mom" appear first using the standard alphabet and that she could make "omm" appear first using the alphabet "abcdefghijklonmpqrstuvwxyz". However, Bessie couldn't figure out any way to make "moo" or "ommnom" appear first. Help Bessie by computing which strings in the input could be lexicographically first by rearranging the order of the alphabet. To compute if string X is lexicographically before string Y find the index of the first character in which they differ, j. If no such index exists then X is lexicographically before Y if X is shorter than Y. Otherwise X is lexicographically before Y if X[j] occurs earlier in the alphabet than Y[j].

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

Input

* Line 1: A single line containing N (1 <= N <= 30,000), the number of strings Bessie is playing with.

* Lines 2..1+N: Each line contains a non-empty string. The total number of characters in all strings will be no more than 300,000. All characters in input will be lowercase characters 'a' through 'z'. Input will contain no duplicate strings.

Output

 * Line 1: A single line containing K, the number of strings that could be lexicographically first.

 * Lines 2..1+K: The (1+i)th line should contain the ith string that could be lexicographically first. Strings should be output in the same order they were given in the input.

Sample Input

 4

omm 

moo 

mom 

ommnom

Sample Output

 2

omm 

mom 


王鉴浩论文题,对所有单词建Trie,然后dfs跑一遍该Trie。能成为最小的字典序的单词,有两个条件,第一个是对应的词尾节点应该在Trie树上的祖先中没有词尾节点。另外我们在跑其中一个单词的时候建一个图,图表示各个字母之间的大小关系。做法是每跑到一个节点,把其他同父子节点的字母向该节点的字母建一条边。然后我们跑到词尾节点的时候,dfs一遍建成的图看有没有环,没环说明能形成有效的字典序。这也是第二个条件:字典的大小关系图无环。

code:

#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
using namespace std;
const int N=3e5+10;
const int M=3e4+10;
const int type=26;
struct node
{
int next[type];
int tag;
}trie[N];
int tot,n,m,ans;
char s[M][310];
int stot;
int infer[M];
int net[type+10][type+10];
void add(char *s,int odr)
{
int now=0;
int len=strlen(s),p;
for(int i=0;i<len;i++)
{
p=s[i]-'a';
if(!trie[now].next[p])
trie[now].next[p]=++tot;
now=trie[now].next[p];
}
trie[now].tag=odr;
return;
}
int vis[type+10];
void init()
{
tot=0;
clr(trie);
stot=0;
clr(infer);
clr(net);
return ;
}
void addedge(int u,int v)
{
net[u][v]++;
return ;
}
void deledge(int u,int v)
{
--net[u][v];
return ;
}
bool dfs(int u)
{
vis[u]=1;
int p;
for(int i=0;i<type;i++)
if(net[u][i])
{
if(vis[i]==1)
return true;
if(vis[i]==2)
continue;
if(dfs(i))
return true;
}
vis[u]=2;
return false;
}
void dfs(int now,int dep)
{
if(trie[now].tag)
{
int flag=0;
clr(vis);
for(int i=0;i<type;i++)
{
if(!vis[i] && dfs(i))
{
flag=1;
break;
}
}
if(!flag)
{
stot++;
infer[trie[now].tag]=1;
}
return ;
}
for(int i=0;i<type;i++)
if(trie[now].next[i])
{
for(int j=0;j<type;j++)
if(trie[now].next[j] && i!=j)
addedge(j,i);
dfs(trie[now].next[i],dep+1);
for(int j=type-1;j>=0;j--)
if(trie[now].next[j] && i!=j)
deledge(j,i);
}
return ;
}
int main()
{
init();
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",s[i]);
add(s[i],i);
}
stot=0;
dfs(0,0);
printf("%d\n",stot);
for(int i=1;i<=n;i++)
if(infer[i])
printf("%s\n",s[i]);
return 0;
}

bzoj3012(Trie)的更多相关文章

  1. 【python】Leetcode每日一题-前缀树(Trie)

    [python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...

  2. LA3942-Remember the Word(Trie)

    题意: 有s个不同的单词,给出一个长字符串把这个字符串分解成若干个单词的连接(可重复使用),有多少种分解方法 分析: dp[i]表示i开始的字符串能分解的方法数 dp[i]=sum(dp[i+len( ...

  3. HDU 1671 Phone List (Trie)

    pid=1671">Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  4. hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)

    统计难题Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submis ...

  5. 字典树(Trie)的java实现

    一.定义 字典树又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用 ...

  6. HDU 4825-Xor Sum(trie)

    题意: 给你一组数,开始询问给一个数  求组中与该数异或值最大的数. 分析:根据异或的特点 要想得到的异或值最大 尽可能的让两个数的每位都相反 先把给定的一组数建树,数的最后一位对应的节点保存这个数的 ...

  7. 【UER #1】跳蚤OS(Trie)

    跳蚤OS 是跳蚤国自主研发的功能强大的操作系统. 跳蚤OS的文件系统与普通的文件系统类似,是个文件夹套文件夹的结构.文件系统根目录称为“//”.我们可以用文件路径来表明文件所在的位置,比如“/flea ...

  8. UVA - 11732 "strcmp()" Anyone? (trie)

    https://vjudge.net/problem/UVA-11732 题意 给定n个字符串,问用strcmp函数比较这些字符串共用多少次比较. strcmp函数的实现 int strcmp(cha ...

  9. hihoCoder 1014 Trie树 (Trie)

    #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho是一对好朋友.出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮 ...

随机推荐

  1. 关于scala 集合 List Map Set

    1,数组 2,List,ListBuffer 3, Map , mutable.Map

  2. bzoj 3343 分块

    因为询问比较少,所以我们可以将n个数分成sqrt(n)个块,每个块用一颗bst存一下,然后对于修改l,r,我们将l,r区间中整块的直接在bst上打一个标签,对于不是整块的我们直接暴力修改,对于询问l, ...

  3. Java常用开发思想与知识点小记(一)

    1.   子类在覆盖父类的方法时,不能抛出比父类更多的异常(儿子不能比父亲干更多的坏事),所以只能捕捉异常,通常在web层捕获异常,给用户一个友好提示. 2.Java内存模型与并发编程三个特性 htt ...

  4. 转 一次完整地http请求

    作者:斯巴达克斯 时间:January 11, 2014 分类:WEB 声明:本文章中的说法仅是个人理解总结,不一定完全正确,但是可以有助于理解. 关于HTTP协议可以参考以下: HTTP协议漫谈 h ...

  5. php中的__call()函数重载

    <?php #调用类中没有的方法时, 会自动调用__call方法重载 #第一个参数是调用时的方法名, 第二个参数为参数组成的数组 class Cat{ public function Hello ...

  6. python中的argparse模块

    argparse干什么用的? 答:参数设置,比如python demo.py -h 诸如此类的. 开始学习这个模块: parser = argparse.ArgumentParser() #使用这个模 ...

  7. python实战===一句python代码搭建FTP服务

    环境搭建: python windows/linux pip install pyftpdlib  (安装失败请到这里下载:https://pypi.python.org/pypi/pyftpdlib ...

  8. selenium===selenium自动化添加日志(转)

    本文转自 selenium自动化添加日志 于logging日志的介绍,主要有两大功能,一个是控制台的输出,一个是保存到本地文件 先封装logging模块,保存到common文件夹命名为logger.p ...

  9. 【Educational Codeforces Round20】

    这场edu有点简单…… 所以题目可能也有点奇奇怪怪的. A.随意构造一下,可以发现只有当填满都不行时才可能无解. #include<bits/stdc++.h> using namespa ...

  10. 经典卷积网络模型 — LeNet模型笔记

    LeNet-5包含于输入层在内的8层深度卷积神经网络.其中卷积层可以使得原信号特征增强,并且降低噪音.而池化层利用图像相关性原理,对图像进行子采样,可以减少参数个数,减少模型的过拟合程度,同时也可以保 ...