题目描述

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个字符串,问哪些串能在特定的字母顺序中字典序最小。

输入输出格式

输入格式:

  • 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.

输出格式:

  • 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.

输入输出样例

输入样例#1:

omm
moo
mom
ommnom
输出样例#1:

omm
mom

说明

The example from the problem statement.

Only "omm" and "mom" can be ordered first.

Solution

要想使一个词语的字典序最小,首先应满足长度尽量短,也就是没有任何一个词构成当前词的前缀

其次是词的每一位都要严格大于与之享有共同前缀的词语

首先对词典建一棵trie

要满足答案的串必须满足其终止节点到根没有其他终止节点,也就是第一个限定

向需要规定大小关系的字符间连边

拓扑排序一下,判环,若无环则可以作为答案输出

(最近尽量写思路清晰但是比较长的程序,其实我是有能力写得很短跑得很快的,但在平时这样似乎没有什么意义,思路清晰最重要吧,毕竟把程序变快是很简单的事)

#include <stdio.h>
#include <memory.h>
#define MaxN 30010
#define MaxL 300010
#define MaxBuf 1<<22
#define Blue() ((S==T&&(T=(S=B)+fread(B,1,MaxBuf,stdin),S==T))?0:*S++)
char B[MaxBuf],*S=B,*T=B;
template<class Type>inline void Rin(Type &x){
x=;int c=Blue();
for(;c<||c>;c=Blue())
;
for(;c>&&c<;c=Blue())
x=(x<<)+(x<<)+c-;
}
inline void geTc(char *C,int &x){
x=;char c=Blue();
for(;c<'a'||c>'z';c=Blue())
;
for(;c>='a'&&c<='z';c=Blue())
*C++=c,x++;
}
bool g[][];
char ch[MaxL],fol[MaxL];
int n,l,pointer,ans,lef[MaxN],in[],out[],o[MaxN];
class Trie{
int ch[MaxL][],root,tot,belong[MaxL];
public:
Trie(){
root=tot=;
memset(ch,,sizeof ch);
memset(belong,,sizeof belong);
}
inline void insert(char *C,int len,int tim){
int at=root;
lef[tim]=pointer;
for(int i=;i<len;i++){
fol[pointer++]=C[i];
if(!ch[at][C[i]-'a'])
ch[at][C[i]-'a']=++tot;
at=ch[at][C[i]-'a'];
}
belong[at]=tim;
}
inline bool design(int i){
int at=root;
for(int j=lef[i];j<lef[i+];j++){
if(belong[at])return false;
int c=fol[j]-'a';
for(int k=;k<;k++)
if(ch[at][k]&&k!=c&&!g[c][k]){
g[c][k]=true;
in[k]++;
out[c]++;
}
at=ch[at][c];
}
return true;
}
}Tree;
namespace enumerate{
bool vis[];
int _que[],hd,tl,tot,sum;
inline bool topsort(){
tot=sum=;
hd=;tl=;
for(int i=;i<;i++){
if(in[i] || out[i])
tot++;
if(!in[i] && out[i]){
_que[++tl]=i; vis[i]=true;
}
else vis[i]=false;
}
while(hd<=tl){
sum++;
int now=_que[hd++];
for(int i=;i<;i++)
if(g[now][i]){
in[i]--;
if(!in[i] && !vis[i]){
_que[++tl]=i; vis[i]=true;
}
}
}
return tot==sum;
}
void main(){
for(int i=;i<=n;i++){
memset(g,false,sizeof g);
memset(in,,sizeof in);
memset(out,,sizeof out);
if(Tree.design(i) && topsort())
o[++ans]=i;
}
}
}
#define FO(x) {freopen(#x".in","r",stdin);}
int main(){
FO(usaco12dec first);
Rin(n);
for(int i=;i<=n;i++){
geTc(ch,l);
Tree.insert(ch,l,i);
}
lef[n+]=pointer;
enumerate::main();
printf("%d\n",ans);
for(int i=;i<=ans;i++){
for(int j=lef[o[i]];j<lef[o[i]+];j++)
putchar(fol[j]);
putchar('\n');
}
return ;
}

[bzoj3012][luogu3065][USACO12DEC][第一!First!] (trie+拓扑排序判环)的更多相关文章

  1. Legal or Not(拓扑排序判环)

    http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others)   ...

  2. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39602   Accepted: 13 ...

  3. LightOJ1003---Drunk(拓扑排序判环)

    One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...

  4. HDU1811 拓扑排序判环+并查集

    HDU Rank of Tetris 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1811 题意:中文问题就不解释题意了. 这道题其实就是一个拓扑排序判圈 ...

  5. Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

    Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...

  6. [USACO12DEC]第一!First! (Trie树,拓扑排序)

    题目链接 Solution 感觉比较巧的题啊... 考虑几点: 可以交换无数次字母表,即字母表可以为任意形态. 对于以其他字符串为前缀的字符串,我们可以直接舍去. 因为此时它所包含的前缀的字典序绝对比 ...

  7. 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环

    [题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一 ...

  8. [Luogu3065][USACO12DEC]第一!First!

    题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...

  9. bzoj 3012: [Usaco2012 Dec]First! Trie+拓扑排序

    题目大意: 给定n个总长不超过m的互不相同的字符串,现在你可以任意指定字符之间的大小关系.问有多少个串可能成为字典序最小的串,并输出这些串.n <= 30,000 , m <= 300,0 ...

随机推荐

  1. 杂项-Java:JeePlus

    ylbtech-杂项-Java:JeePlus 一个集成了代码生成器的java快速开发框架 1. 介绍返回顶部 1. 响应式开发 JeePlus采用了目前极为流行的扁平化响应式的设计风格,UI框架使用 ...

  2. 栗染-Jsp编码常见问题

    如图在我们新建一个jsp的时候想给自己的页面加一个中文就会出现如图所示的问题 遇到这种情况一般是选第二个或者 将<%@ page language="java" import ...

  3. bzoj 1700: [Usaco2007 Jan]Problem Solving 解题【dp】

    很像贪心的dp啊 这个定金尾款的设定让我想起了lolita和jk制服的尾款地狱-- 设f[i][j]为从j到i的付定金的最早月份然后从f[k][j-1]转移来,两种转移f[i][j]=min(f[i] ...

  4. bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路【最小生成树】

    先把已有的边并查集了,然后MST即可 记得开double #include<iostream> #include<cstdio> #include<algorithm&g ...

  5. c++ rand && srand 函数

    RAND: 结构:rand()     注:rand()不需要参数,它会根据种子返回一个从0到最大随机数的任意整数. 作用:生成一个随机数. 头文件:#include <cstdlib> ...

  6. python orm / 表与model相互转换

    orm英文全称object relational mapping,就是对象映射关系程序,简单来说我们类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却都是关系型的,为了保证一致的 ...

  7. Get 和 Post 使用篇(1)

    1.Post 请求发送方式 实例: const string sResponseEncoding = "gb2312"; //测试文本信息 string postText = &q ...

  8. hdu5924Mr. Frog’s Problem

    Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  9. 学习笔记 第五章 使用CSS美化网页文本

    第五章   使用CSS美化网页文本 学习重点 定义字体类型.大小.颜色等字体样式: 设计文本样式,如对齐.行高.间距等: 能够灵活设计美观.实用的网页正文版式. 5.1 字体样式 5.1.1 定义字体 ...

  10. Python :用两个栈实现队列

    转自:http://blog.csdn.net/Lynette_bb/article/details/75092745 牛客网上的剑指 offer的在线编程: 题目描述 用两个栈来实现一个队列,完成队 ...