bzoj3012 [Usaco2012 Dec]First!
来源:互联网 发布:南京软件测试工资 编辑:程序博客网 时间:2024/06/05 17:14
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].
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
4omm moo mom ommnom INPUT DETAILS: The example from the problem statement.
Sample Output
2omm mom OUTPUT DETAILS: Only "omm" and "mom" can be ordered first.
Key To Problem
我们考虑一个串是否能排在第一位,那么对于与它前缀相同的字符串,去掉相同前缀它的优先级依旧是最大的。
那么就想到可以用trie树处理,首先将所有的字符串加入trie树中,对于字符串s,假设它可以排在第一位,那么它的字符x的优先级一定大于和它相同父节点的字符y的优先级。
所以我们可以由x向y连一条有向边,用topsort判环,若有环存在,那么字符串s不能排在第一位,反之则可以排在第一位。
这里我要d一下Bosh,他嘲讽我删边的方法太暴力了,之后他的程序跑完是9604 ms,我的程序是892 ms,233333
Hint
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 30010#define M 300#define K 30using namespace std;struct node{ int next[26]; int sum; bool x;};node a[N<<3];int n,tot,top,szy;bool flag;char s[N][M];int ans[N];bool used[K];int in[K];int head[K];int next[(K*K)<<1];int tail[(K*K)<<1];void mem(){ top=0; memset(head,0,sizeof(head)); memset(next,0,sizeof(next)); memset(tail,0,sizeof(tail)); memset(used,0,sizeof(used)); memset(in,0,sizeof(in)); flag=false;}void add(int x,int y){ next[++top]=head[x]; head[x]=top; tail[top]=y;}void insert(char ch[]){ int len=strlen(ch+1); int now=0; for(int i=1;i<=len;i++) { int k=ch[i]-'a'; if(!a[now].next[k]) a[now].next[k]=++tot; now=a[now].next[k]; } a[now].x=true;}void query(char ch[]){ int len=strlen(ch+1); int now=0; for(int i=1;i<=len;i++) { int k=ch[i]-'a'; if(a[now].x) { flag=true; return ; } for(int j=0;j<26;j++) if(j!=k&&a[now].next[j]) add(k,j),in[j]++; now=a[now].next[k]; }}void topsort(){ queue<int>Q; for(int i=0;i<26;i++) if(!in[i]) Q.push(i); while(!Q.empty()) { int k=Q.front(); Q.pop(); for(int i=head[k];i;i=next[i]) { int to=tail[i]; if(!in[to]) { flag=true; return ; } in[to]--; if(!in[to]) Q.push(to); } } for(int i=0;i<26;i++) if(in[i]) flag=true;}int main(){// freopen("str.in","r",stdin);// freopen("str.out","w",stdout); cin>>n; for(int i=1;i<=n;i++) { scanf("%s",s[i]+1); insert(s[i]); } for(int i=1;i<=n;i++) { mem(); query(s[i]); topsort(); if(!flag) ans[++szy]=i; } cout<<szy<<endl; for(int i=1;i<=szy;i++) printf("%s\n",s[ans[i]]+1); return 0;}//最后orz s爷
- bzoj3012 [Usaco2012 Dec]First!
- 【bzoj3012】[Usaco2012 Dec]First!
- 【bzoj3012】[Usaco2012 Dec]First! trie+拓扑排序
- bzoj3012[Usaco2012 Dec]First! 一号单词
- 【bzoj3012】[Usaco2012 Dec]First(题解)
- BZOJ 3012/Luogu 3065 [Usaco2012 Dec]First!
- [bzoj3012][Usaco2015 Dec][字典树][Top序]First!
- BZOJ 3012: [Usaco2012 Dec]First! 字典树 拓扑排序
- BZOJ 3012 [Usaco2012 Dec]First! wzq脑洞hash树(正解trie树)
- 3011: [Usaco2012 Dec]Running Away From the Barn
- 【bzoj3011】[Usaco2012 Dec]Running Away From the Barn
- BZOJ 3011 Usaco2012 Dec Running Away From the Barn 可并堆
- 【BZOJ3011】[Usaco2012 Dec]Running Away From the Barn【可并堆】
- [可并堆] BZOJ 3011 [Usaco2012 Dec]Running Away From the Barn
- [BZOJ3011][Usaco2012 Dec]Running Away From the Barn(可并堆)
- bzoj 3011: [Usaco2012 Dec]Running Away From the Barn (可并堆)
- 【BZOJ 3011】[Usaco2012 Dec]Running Away From the Barn 可并堆
- BZOJ3011: [Usaco2012 Dec]Running Away From the Barn 可并堆
- Featherstone R. Rigid body dynamics algorithms page 9
- nginx之平滑升级详解
- MongoDB Shell使用
- [Leetcode] Integer to Roman
- C++ STL(标准模板类) 学习笔记
- bzoj3012 [Usaco2012 Dec]First!
- 求同一号码一天内的上网流量
- leetcode 3Longest Substring Without Repeating Characters
- UI07_导航视图控制器
- 怎么修改PDF文件中的文字
- 访问Web Service(第二讲)
- android自定义view——温度计
- ios学习--TableView详细解释
- c#特性加反射实现解耦