题目描述

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

    -by luogu

http://daniu.luogu.org/problem/show?pid=3065



字典(trie)树,没什么好讲的;

建字典树后,如何check单词?

首先性质1:

  一个单词的前缀是单词的单词非法;

--显然,无论如何规定字典序,空就是最高的;

然后在字典树上i位置选j字母——意味着我们认为j字母比i的其他子节点小,假设k字母正是这样一个子节点;

这样的话,继续走下去时,如果在某个节点时,我们想走k字母但是发现j字母也是这个节点的儿子,那我们就走不了k了;

然后这个单词非法;

可以考虑用拓扑排序的思想规定大小;

(如果可以走j,则其它子节点字母向j连有向边构成图,然后check就是从一个节点dfs,若遍历到一个点,他也是当前父节点的一个儿子,则单词非法,先check再建边);

我一开始觉得这个方法效率玄学;

然而其实图中无环所以dfs是单次O(26)的,于是,总效率是O(num*26)的(num总字符数)

十分合适;

代码如下:

 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
struct Trie{
int ch[];
int flag,size;
};
Trie trie[];
int n,tot;
string word[];
string word2[];
int ans;
int e[][];
void Init();
void bui_trie();
void check(int );
int dfs(int ,int );
int main()
{
int i,j,k;
Init();
scanf("%d",&n);
bui_trie();
for(i=;i<=n;i++){
memset(e,,sizeof(e));
check(i);
}
printf("%d\n",ans);
for(i=;i<=ans;i++)
cout<<word2[i]<<'\n';
}
void Init(){
memset(trie,,sizeof(trie));
n=tot=ans=;
}
void bui_trie(){
int i,j,k;
for(i=;i<=n;i++){
cin>>word[i];k=;
for(j=;j<word[i].size();j++){
if(!trie[k].ch[word[i][j]-'a'])
trie[k].ch[word[i][j]-'a']=++tot;
k=trie[k].ch[word[i][j]-'a'];
}
trie[k].flag=;
}
}
void check(int x){
int i,j,k=,l;
for(i=;i<word[x].size();i++){
if(trie[k].flag)return;
j=trie[k].ch[word[x][i]-'a'];
if(dfs(word[x][i]-'a',k))
return;
for(l=;l<=;l++)
if(trie[k].ch[l]&&l!=word[x][i]-'a')
e[l][++e[l][]]=word[x][i]-'a';
k=j;
}
word2[++ans]=word[x];
}
int dfs(int now,int k){
int re=,i;
for(i=;i<=e[now][];i++)
if(!trie[k].ch[e[now][i]]&&!re)
re=dfs(e[now][i],k);
else
return ;
return re;
}

luogu P3065 first——trie树相关的更多相关文章

  1. [SCOI2016]背单词——trie树相关

    题目描述 Lweb 面对如山的英语单词,陷入了深深的沉思,”我怎么样才能快点学完,然后去玩三国杀呢?“.这时候睿智的凤老师从远处飘来,他送给了 Lweb 一本计划册和一大缸泡椒,他的计划册是长这样的: ...

  2. Trie树相关博客

    1. c++代码实现,包含删除操作:https://www.cnblogs.com/luxiaoxun/archive/2012/09/03/2668611.html 2. 一种典型实现及简单分析:h ...

  3. Trie树-提高海量数据的模糊查询性能

    今天这篇文章源于上周在工作中解决的一个实际问题,它是个比较普遍的问题,无论做什么开发,估计都有遇到过.具体是这样的,我们有一份高校的名单(2657个),需要从海量的文章标题中找到包含这些高校的标题,其 ...

  4. 洛谷P3065 [USACO12DEC]第一!First!(Trie树+拓扑排序)

    P3065 [USACO12DEC]第一!First! 题目链接:https://www.luogu.org/problemnew/show/P3065 题目描述 Bessie一直在研究字符串.她发现 ...

  5. AC自动机相关Fail树和Trie图相关基础知识

    装载自55242字符串AC自动机专栏 fail树 定义 把所有fail指针逆向,这样就得到了一棵树 (因为每个节点的出度都为1,所以逆向后每个节点入度为1,所以得到的是一棵树) 还账- 有了这个东西, ...

  6. Luogu P2922 [USACO08DEC]秘密消息Secret Message 字典树 Trie树

    本来想找\(01Trie\)的结果找到了一堆字典树水题...算了算了当水个提交量好了. 直接插入模式串,维护一个\(Trie\)树的子树\(sum\)大小,求解每一个文本串匹配时走过的链上匹配数和终点 ...

  7. LUOGU P2580 于是他错误的点名开始了(trie树)

    传送门 解题思路 trie树模板

  8. luogu P6088 [JSOI2015]字符串树 可持久化trie 线段树合并 树链剖分 trie树

    LINK:字符串树 先说比较简单的正解.由于我没有从最简单的考虑答案的角度思考 所以... 下次还需要把所有角度都考察到. 求x~y的答案 考虑 求x~根+y~根-2*lca~根的答案. 那么问题变成 ...

  9. [luogu P3065] [USACO12DEC]第一!First!

    [luogu P3065] [USACO12DEC]第一!First! 题目描述 Bessie has been playing with strings again. She found that ...

随机推荐

  1. php-fpm epoll封装

    参考 http://www.jianshu.com/p/dac223d7d9ad 事件对象结构 //fpm_event.h struct fpm_event_s { int fd; /* IO 文件句 ...

  2. [Alpha]团队任务拆解

    要求 团队任务拆解 Alpha阶段总体规划 初步实现测试.报告: 实现对游戏最基本的测试,包括内置随机测试.提供可供选择的组合测试 实现对游戏测试时操作的记录并最终生成报告 能够在发现异常时及时将异常 ...

  3. JDK7 AutoCloseable

    干嘛的 直接看JDK7的流(运用了AutoCloseable)源码 public abstract class InputStream implements Closeable { //实现Close ...

  4. Mac下像Windows那样带有预览图的快速切换-HyperSwitch

    这东西是免费的,他家还出了一个HyperDock的收费软件. 下载:https://bahoom.com/hyperswitch/get 离线版本:(链接: https://pan.baidu.com ...

  5. 删除数据库的数据后让id从1开始算

    delete from t_AttendanceRecorddbcc checkident('t_AttendanceRecord',reseed,0) truncate table  表名称

  6. springmvc执行原理及自定义mvc框架

    springmvc是spring的一部分,也是一个优秀的mvc框架,其执行原理如下: (1)浏览器提交请求经web容器(比如tomcat)转发到中央调度器dispatcherServlet. (2)中 ...

  7. Cloudera Manager安装之Cloudera Manager 5.3.X安装(三)(tar方式、rpm方式和yum方式)

    不多说,直接上干货! 福利每天都有  =>  =>=>=>=> 欢迎大家,关注微信扫码并加入我的4个微信公众号:   大数据躺过的坑      Java从入门到架构师  ...

  8. 【开源组件】FastDFS极速入门与安装

    FastDFS是一个开源的轻量级的分布式文件系统,为互联网量身定制,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供 ...

  9. springboot-20-全局异常处理

    springboot的全局异常处理 . 新建一个类GlobalDefaultExceptionHandler 在class上注解 @ControllerAdvice 方法上注解 @ExceptionH ...

  10. 各种数据库maven的pom文件编写与ibernate链接配置

    各种数据库Hibernate链接配置 Derby db driver maven dependency <dependency>         <groupId>org.ap ...