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 ...
随机推荐
- multiprocessor(下)
一.数据共享 展望未来,基于消息传递的并发编程是大势所趋即便是使用线程,推荐做法也是将程序设计为大量独立的线程集合,通过消息队列交换数据.这样极大地减少了对使用锁定和其他同步手段的需求,还可以扩展到分 ...
- Flowportal-BPM——环境配置
环境配置: 一.控制面板→程序和功能→打开或不关闭Window功能→选择选项 二.控制面板→管理工具→Internet信息服务(IIS)管理器→左侧第一个→ISAPI和CGI限制→全部选为[允许] 三 ...
- (Lua) C++ 呼叫 Lua 的變數、函式
簡單的在C++裡頭與Lua交互操作 首先提供 Lua 的簡單範例 print(" Lua 2019/01/07 !!!") -- Variable monster_type = & ...
- 使用NHibernate(5)-- Linq To NHibernate
Linq是NHibernate所支持的查询语言之一,对于Linq的实现在源码的src/Linq目录下.以下是一个使用Linq进行查询数据的示例: var users = session.Query&l ...
- elastic-job动态添加定时任务
在elastic-job的使用过程中,我们会遇到动态添加定时任务的时候,但是官网上面并没有对这块内容进行说明.按照我的理解以及官网上面elastic-job的框架图,ej的定时任务其实是存储在zook ...
- str_split 分隔中文出现乱码 替代函数
function mbstringtoarray($str,$charset) { $strlen=mb_strlen($str); while($strlen){ $array[]=mb_subst ...
- C# virtual abstract
virtual和abstract都是用来修饰父类的,通过覆盖父类的定义,让子类重新定义. 它们有一个共同点:如果用来修饰方法,前面必须添加public,要不然就会出现编译错误:虚拟方法或抽象方法是不能 ...
- 有关索引的DMV
转自:http://www.cnblogs.com/CareySon/archive/2012/05/17/2505981.html#commentform 有关索引的DMV 1.查看那些被大量更新, ...
- Mathematik
Ausdruck auf Deutsch Lösen Problem der Abteilung. 求导. Die Abteilung von 3x ist 3. 3x的导数是3 Lösen Prob ...
- ACM,算法
ACM,算法 描述 最近Topcoder的XD遇到了一个难题,倘若一个数的三次方的后三位是111,他把这样的数称为小光棍数.他已经知道了第一个小光棍数是471,471的三次方是104487111,现在 ...