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 ...
随机推荐
- 2016级算法期末上机-C.简单·Bamboo's Fight with DDLs III
简单·Bamboo's Fight with DDLs III 分析 一句话:贪心,简单哈夫曼应用,要求的其实是所有结点的值与权值的乘积之和,也就是带权路径长. 可以理解为非叶子节点的权值的和,这里的 ...
- FlowPortal-BPM——移动手机端配置与IIS发布
一.移动手机端配置 (1)VS打开文件夹iAnyWhere,配置config文件 (2)BPM-Web文件config中设置(设置为外网网址) 二.BPM设置 勾选移动审批可以设置要展示的字段信息,修 ...
- BZOJ3168. [HEOI2013]钙铁锌硒维生素(线性代数+二分图匹配)
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=3168 题解 首先,我们需要求出对于任意的 \(i, j(1 \leq i, j \leq ...
- 05-树9 Huffman Codes (30 分)
In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redunda ...
- 基于nightmare的美团美食商家爬虫实践
前言美团商家页分析需要爬取的数据有(这里没有按人数爬)爬虫工具选取pysipderscrapynightmare同步任务js动态加载中断继续爬坑总结示例代码 前言 上学的时候自己写过一些爬虫代码,比较 ...
- (转).NET技术大系概览 (迄今为止最全的.NET技术栈)
前言 .Net推出13年了,Visual Studio 2015 / .NET Framework 4.6昨天也发布了. 从2002年的.NET 1.0开始,1.1,2.x,3.x,4.x,每个新版本 ...
- (转)[小工具] Linux下列格式化工具 - column
当你看到Linux下命令输出的列歪歪扭扭时,是不是看着很不爽?column 命令就可以方便地解决这个问题. 比如: 我们一般就只用到上面这一个用法. column的其他用法如下: 选项 含义 -c 字 ...
- (转)zabbix之生产案例
原文: https://www.abcdocker.com/abcdocker/category/zabbix/ 原文: https://chegva.com/1170.html
- Thinkphp3.2.X自动生成应用目录
从3.2.2版本开始,可以支持自动生成默认模块之外的模块目录以及批量生成控制器和模型类. 例如,如果我们需要生成一个Admin模块用于后台应用,在应用入口文件中定义如下: // 绑定Admin模块到当 ...
- Linux 命令 su 和 sudo 比较
su : 可以用来切换到任何用户,但是要求输入目标用户的密码 比如:su - su - root (等价 su -) su - user su root 与 su - root 的区别在于,后者不切换 ...