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. PHP正则 贪婪匹配与非贪婪匹配

    $str = ".abcdeabcde"; preg_match('/a.+?e/', $str, $match); print_r($match); Array ( [0] =& ...

  2. POJ 1321 棋盘问题 (深搜)

    题目链接 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆 ...

  3. Winform MD5

    1:MD5 http://www.cmd5.com/ 字节数组----字符串 //将字节数组中每个元素按照指定的编码格式解析成字符串//直接将数组ToString()//将字节数组中的每个元素ToSt ...

  4. fragment+tabhost与viewpager

    学到哪里写到哪里吧 A.viewpager a.用V4包中的fragment,activity继承FragmentActivity b.布局中加入<android.support.v4.view ...

  5. Codeforces Round #483 (Div. 1) 简要题解

    来自FallDream的博客,未经允许,请勿转载,谢谢. 为了证明一下我又来更新了,写一篇简要的题解吧. 这场比赛好像有点神奇,E题莫名是道原题,导致有很多选手直接过掉了(Claris 表演24s过题 ...

  6. linux平台学x86汇编语言学习集合帖

    linux平台学x86汇编语言学习集合帖 linux平台学x86汇编(一):https://blog.csdn.net/shallnet/article/details/45543237 linux平 ...

  7. sublime Text3 === 无法输入input的问题解决办法

    sublimetext无法对input或者raw_input执行.因此搜了很多方法后,解决了这个问题: 1.先下载插件sublimerepl ,如果无法下载,请点击https://github.com ...

  8. oracle to_char 返回毫秒级

    select to_char(systimestamp,'yyyy-mm-dd hh24:mi:ssxff') time1, 关键在 systimestamp

  9. 1833: [ZJOI2010]count 数字计数——数位dp

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1833 省选之前来切一道裸的数位dp.. 题意 统计[a,b]中0~9每个数字出现的次数(不算 ...

  10. c语言实现CRC校验和

    最近在摄像头采集的数据清晰度上需要加强,则在每一帧传输的数据包后边加了CRC校验和.CRC校验和有16位的,也有32位的.至于CRC校验和算法原理,我是在百度上学习的,其实网上有很多这种资料.简单的说 ...