[bzoj3012][Usaco2015 Dec][字典树][Top序]First!
来源:互联网 发布:python中字符串的 编辑:程序博客网 时间:2024/06/04 18:28
Description
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
题解
Tire+top序
考虑单词可以成为第一的情况1、没有任何单词是他的前缀。因为如果他的某一前缀是一个单词的话,那么他一定不是第一个
2、他的所有字母一定比和他在Tire树中拥有同一个父亲的兄弟高级首先对单词建Tire树,记录单词在Tire树中出现的位置
枚举每个单词,先在Tire树中找到这个单词的位置。如果在找到这个单词的位置之前,发现了另一个单词。那么排除这一个单词。因为有单词成为他的前缀了
倘若找到了这个单词的位置,那么在寻找的过程中我们建top序
对于x,假设x>y这个字母,我们就x -> y连一条边。之后跑top序判环就好
#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>using namespace std;struct node{ int x,y,next;}a[1111000];int len,last[27];void ins(int x,int y){ len++;a[len].x=x;a[len].y=y; a[len].next=last[x];last[x]=len;}struct Tire{ int c[27],s; Tire(){memset(c,-1,sizeof(c));}}tr[311000];int tot,root;void add(char *st,int op){ int len=strlen(st+1),x=root; for(int i=1;i<=len;i++) { int y=st[i]-'a'+1; if(tr[x].c[y]==-1)tr[x].c[y]=++tot; x=tr[x].c[y]; } tr[x].s=op;}int ru[27];bool v[27];bool top(){ memset(v,true,sizeof(v)); for(int i=1;i<=26;i++) { int x=0; for(int j=1;j<=26;j++)if(ru[j]==0 && v[j]==true){x=j;break;} if(x==0)return false; v[x]=false; for(int k=last[x];k;k=a[k].next)ru[a[k].y]--; } return true;}int ans[31000],cnt,n;int tmp[31000],now;//记录每个串开头char ss[310000],s[310000];//串接起来的结果 int main(){ scanf("%d",&n); now=root=0; for(int i=1;i<=n;i++) { scanf("%s",ss+1); add(ss,i);tmp[i]=now+1; int len=strlen(ss+1); for(int j=1;j<=len;j++)s[++now]=ss[j]; } cnt=0;tmp[n+1]=now+1; for(int i=1;i<=n;i++)//枚举每个单词 { len=0;memset(last,0,sizeof(last));memset(ru,0,sizeof(ru)); int x=root;bool bk=false; for(int j=tmp[i];j<tmp[i+1];j++) { int y=s[j]-'a'+1; if(tr[x].s>0){bk=true;break;} //x=tr[x].c[y]; for(int k=1;k<=26;k++)if(tr[x].c[k]!=-1 && k!=y){ins(y,k);ru[k]++;} x=tr[x].c[y]; } if(bk==true)continue;//前面有比我更短的 if(top())ans[++cnt]=tr[x].s; } printf("%d\n",cnt); for(int i=1;i<=cnt;i++) { for(int j=tmp[ans[i]];j<tmp[ans[i]+1];j++)printf("%c",s[j]); printf("\n"); } return 0;}
- [bzoj3012][Usaco2015 Dec][字典树][Top序]First!
- bzoj3012 [Usaco2012 Dec]First!
- 【bzoj3012】[Usaco2012 Dec]First!
- 【bzoj3012】[Usaco2012 Dec]First! trie+拓扑排序
- bzoj3012[Usaco2012 Dec]First! 一号单词
- 【bzoj3012】[Usaco2012 Dec]First(题解)
- BZOJ 3012: [Usaco2012 Dec]First! 字典树 拓扑排序
- bzoj4394【Usaco2015 Dec】Bessie
- 【Usaco2015 dec 】Counting Haybales
- [Usaco2015 dec]Max Flow
- bzoj 4397: [Usaco2015 dec]Breed Counting 前缀和/线段树
- bzoj4397【Usaco2015 Dec】Breed Counting
- bzoj4393【Usaco2015 Dec】Fruit Feast
- bzoj4392【Usaco2015 Dec】Counting Haybales
- bzoj4390【Usaco2015 Dec】Max Flow
- 【bzoj4390】[Usaco2015 dec]Max Flow
- bzoj4397: [Usaco2015 dec]Breed Counting
- bzoj4390: [Usaco2015 dec]Max Flow
- Netty学习心得 netty服务端和客户端的连接
- C 位域
- 算法:从键盘输入能够构成三角形的三条边长,编程计算该三角形的面积
- 文件COPY
- android 编辑标签,在一个文本框输入标签回车添加退格删除
- [bzoj3012][Usaco2015 Dec][字典树][Top序]First!
- 1102. Invert a Binary Tree (25)
- Java中Servlet的使用(二)
- S5PV210开发 -- Nand和e-MMC区别以及系统更新
- 如何安装单节点的hadoop
- java8新特性
- ubifs文件系统的移植
- 关于Android安卓APP保活
- PHP的常见ORM特性初探