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. Response.Redirect在新窗口打开(转载)

    Response.Rederect在默认情况下是在本页跳转,所以除了在js中用window.open或是给A标签添加target属性之外,在后台似乎不能来打开新的页面,其实不然,通过设置form的ta ...

  2. Centos修改镜像为国内的阿里云源或者163源等国内源

    阿里安装软件镜像源 阿里云Linux安装镜像源地址:http://mirrors.aliyun.com/ 第一步:备份你的原镜像文件,以免出错后可以恢复. mv /etc/yum.repos.d/Ce ...

  3. ubuntu中使用virtualbox遇到Kernel driver not installed (rc=-1908)错误

    百度之后得到解决,再此做个笔记 错误提示 Kernel driver not installed (rc=-1908) The VirtualBox Linux kernel driver (vbox ...

  4. 关于EditText.setText()无法显示的问题

    将EditText在初始化后调用EditText.setSaveEnabled(false); 让Android 系统不保存值,这样就不会恢复了.

  5. 以下suse11.3x64可以安装pycrypto-2.6.1

    rpm -qa adaptec-firmware-1.35-2.15.4gnome-menus-branding-SLED-11.1-14.26man-pages-3.15-2.23.1crackli ...

  6. Linux 入门记录:十五、Linux 网络基本配置

    一.以太网(Ethernet) 以太网(Ethernet)是一种计算机局域网技术.IEEE 组织的 IEEE 802.3 标准制定了以太网的技术标准,它规定了包括物理层的连线.电子信号和介质访问层协议 ...

  7. 10 个打造 React.js App 的最佳 UI 框架

    10 个打造 React.js App 的最佳 UI 框架 在本文中,我们将分享一些助你打造 React.js App 最佳的 UI 框架.它们具备你所需要的基本 React 组件,以及易用的 API ...

  8. C#杂七杂八记录

     1. 日期格式表示 DateTime.Now.ToString("yyyy-MM-dd")  2. div跟屏幕的高度一样高,自适应 <style> html, bo ...

  9. 欢迎访问新博客aiyoupass.com

    新博客基本搭建好了,欢迎访问.aiyoupass.com

  10. java常用设计模式学习心得

    学习自:http://shenzhenchufa.blog.51cto.com/730213/161581 代码来自:http://shenzhenchufa.blog.51cto.com/73021 ...