bzoj3012(Trie)
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)的更多相关文章
- 【python】Leetcode每日一题-前缀树(Trie)
[python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...
- LA3942-Remember the Word(Trie)
题意: 有s个不同的单词,给出一个长字符串把这个字符串分解成若干个单词的连接(可重复使用),有多少种分解方法 分析: dp[i]表示i开始的字符串能分解的方法数 dp[i]=sum(dp[i+len( ...
- HDU 1671 Phone List (Trie)
pid=1671">Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
- 字典树(Trie)的java实现
一.定义 字典树又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用 ...
- HDU 4825-Xor Sum(trie)
题意: 给你一组数,开始询问给一个数 求组中与该数异或值最大的数. 分析:根据异或的特点 要想得到的异或值最大 尽可能的让两个数的每位都相反 先把给定的一组数建树,数的最后一位对应的节点保存这个数的 ...
- 【UER #1】跳蚤OS(Trie)
跳蚤OS 是跳蚤国自主研发的功能强大的操作系统. 跳蚤OS的文件系统与普通的文件系统类似,是个文件夹套文件夹的结构.文件系统根目录称为“//”.我们可以用文件路径来表明文件所在的位置,比如“/flea ...
- UVA - 11732 "strcmp()" Anyone? (trie)
https://vjudge.net/problem/UVA-11732 题意 给定n个字符串,问用strcmp函数比较这些字符串共用多少次比较. strcmp函数的实现 int strcmp(cha ...
- hihoCoder 1014 Trie树 (Trie)
#1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho是一对好朋友.出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮 ...
随机推荐
- 【51NOD】1201 整数划分
[题意]将n划分成不同正整数的和的方案数. [算法]动态规划 [题解] 暴力:f[i][j]:只用前1..i的数字,总和为j的方案数 本质上是01背包,前i个物体,总质量为j的方案数 f[i][j]= ...
- 课下加分项目 MYPWD 20155335 俞昆
Mypwd 的解读与实现 20155335 linux下pwd命令的编写 实验要求: 1 .学习pwd命令 2 . 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 .实现my ...
- 为什么Javascript有设计缺陷
1. 设计阶段过于仓促 Javascript的设计,其实只用了十天.而且,设计师是为了向公司交差,本人并不愿意这样设计(参见<Javascript诞生记>). 另一方面,这种语言的设计初衷 ...
- 再思linux内核在中断路径内不能睡眠/调度的原因(2010)【转】
转自:http://blog.csdn.net/maray/article/details/5770889 Linux内核中断路径中不能睡眠,为什么? 这里就行了很深入的讨论,值得一看:http:// ...
- 64_g1
GAPDoc-1.5.1-12.fc26.noarch.rpm 13-Feb-2017 22:37 1082286 GAPDoc-latex-1.5.1-12.fc26.noarch.rpm 13-F ...
- C后端设计开发 - 第3章-气功-原子锁线程协程
正文 第3章-气功-原子锁线程协程 后记 如果有错误, 欢迎指正. 有好的补充, 和疑问欢迎交流, 一块提高. 在此谢谢大家了. 童话镇 - http://music.163.com/#/m/song ...
- MYSQL有外键无法删除
今天删除数据库中数据,提示因为设置了foreign key,无法修改删除 可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况. SET FOREIGN_KEY_CHECKS=0; 删除 ...
- List转换为DataTable List<Entity>
/// <summary> /// 将List转换成DataTable /// </summary> /// <typeparam name="T"& ...
- hdu 1269 迷宫城堡(Targin算法)
---恢复内容开始--- 迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- MVC - 13.验证
1.注解验证-Required-StringLength-Range-Regular. 1.1.验证方式 [Required], [StringLength], [Range], 和 [Regular ...