luogu P3065 first——trie树相关
题目描述
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树相关的更多相关文章
- [SCOI2016]背单词——trie树相关
题目描述 Lweb 面对如山的英语单词,陷入了深深的沉思,”我怎么样才能快点学完,然后去玩三国杀呢?“.这时候睿智的凤老师从远处飘来,他送给了 Lweb 一本计划册和一大缸泡椒,他的计划册是长这样的: ...
- Trie树相关博客
1. c++代码实现,包含删除操作:https://www.cnblogs.com/luxiaoxun/archive/2012/09/03/2668611.html 2. 一种典型实现及简单分析:h ...
- Trie树-提高海量数据的模糊查询性能
今天这篇文章源于上周在工作中解决的一个实际问题,它是个比较普遍的问题,无论做什么开发,估计都有遇到过.具体是这样的,我们有一份高校的名单(2657个),需要从海量的文章标题中找到包含这些高校的标题,其 ...
- 洛谷P3065 [USACO12DEC]第一!First!(Trie树+拓扑排序)
P3065 [USACO12DEC]第一!First! 题目链接:https://www.luogu.org/problemnew/show/P3065 题目描述 Bessie一直在研究字符串.她发现 ...
- AC自动机相关Fail树和Trie图相关基础知识
装载自55242字符串AC自动机专栏 fail树 定义 把所有fail指针逆向,这样就得到了一棵树 (因为每个节点的出度都为1,所以逆向后每个节点入度为1,所以得到的是一棵树) 还账- 有了这个东西, ...
- Luogu P2922 [USACO08DEC]秘密消息Secret Message 字典树 Trie树
本来想找\(01Trie\)的结果找到了一堆字典树水题...算了算了当水个提交量好了. 直接插入模式串,维护一个\(Trie\)树的子树\(sum\)大小,求解每一个文本串匹配时走过的链上匹配数和终点 ...
- LUOGU P2580 于是他错误的点名开始了(trie树)
传送门 解题思路 trie树模板
- luogu P6088 [JSOI2015]字符串树 可持久化trie 线段树合并 树链剖分 trie树
LINK:字符串树 先说比较简单的正解.由于我没有从最简单的考虑答案的角度思考 所以... 下次还需要把所有角度都考察到. 求x~y的答案 考虑 求x~根+y~根-2*lca~根的答案. 那么问题变成 ...
- [luogu P3065] [USACO12DEC]第一!First!
[luogu P3065] [USACO12DEC]第一!First! 题目描述 Bessie has been playing with strings again. She found that ...
随机推荐
- C#-进制转化
(一)数制 计算机中采用的是二进制,因为二进制具有运算简单,易实现且可靠,为逻辑设计提供了有利的途径.节省设备等优点,为了便于描述,又常用八.十六进制作为二进制的缩写.一般计数都采用进位计数,其特点是 ...
- Linux磁盘分区、挂在
分区基础知识分区的方式:1) mbr分区:1.最多支持四个主分区2.系统只能安装在主分区3.扩展分区要占一个主分区4.MBR最大只支持2TB,但拥有最好的兼容性2) gtp分区:1.支持无限多个主分区 ...
- 平衡树 替罪羊树(Scapegoat Tree)
替罪羊树(Scapegoat Tree) 入门模板题 洛谷oj P3369 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入xx数 删除xx数(若有多个相同 ...
- 快速沃尔什变换 FWT 学习笔记【多项式】
〇.前言 之前看到异或就担心是 FWT,然后才开始想别的. 这次学了 FWT 以后,以后判断应该就很快了吧? 参考资料 FWT 详解 知识点 by neither_nor 集训队论文 2015 集合幂 ...
- 通过MSI解压缩Cab文件
迁移自我的Github 如果只是想做类似解压缩的操作,那么可以使用如下命令行 C:\Windows\System32\expand.exe <cab file path> -F:* < ...
- SqlServer索引、优化、约束、连接
索引的创建和删除 create index in_name on person(name) --创建索引 drop index person.in_name --删除索引 create index i ...
- (转)drbd详解
原文:http://blog.csdn.net/u014421556/article/details/52925442
- github里如何删除一个repository仓库
高手请绕行,新手往下走. 作为一个刚接触github(https://github.com/)的新手,除了感叹开源的丰富和强大之外,自己肯定也想试用一下,因此申请帐号神马的.今天自己创建一个Repos ...
- 【Eclipse】在Project Explore中隐藏不需要显示的文件
右击项目->Resource->Resource Filters->AddFilter type: Exclude allApplies to : Files (All childr ...
- php 判断字符串之间包含关系
之前常用stristr , strpos判断. 因为处理1000W * 1000W级别,循环就是漫长漫长... 在此,对stristr, strpos, explode判断字符串包含关系处理速度对比 ...