BZOJ_3448_[Usaco2014 Feb]Auto-complete_Trie

Description

Bessie the cow has a new cell phone and enjoys sending text messages, although she keeps making spelling errors since she has trouble typing on such a small screen with her large hooves. Farmer John has agreed to help her by writing an auto-completion app that takes a partial word and suggests how to complete it. The auto-completion app has access to a dictionary of W words, each consisting of lowercase letters in the range a..z, where the total number of letters among all words is at most 1,000,000. The app is given as input a list of N partial words (1 <= N <= 1000), each containing at most 1000 lowercase letters. Along with each partial word i, an integer K_i is also provided, such that the app must find the (K_i)th word in alphabetical order that has partial word i as a prefix. That is, if one ordered all of the valid completions of the ith partial word, the app should output the completion that is (K_i)th in this sequence.

XYW在和他的男人聊天的时候经常会因为打字速度太慢而被鄙视,于是他想开发一种自动补全软件。开始他有一个包含了w个词的字典(总长度不大于10^6)。接下来他想知道在字典中可以由给定的字符串s加上若干个字母得到的字符串字典序第k大的编号是多少?有n组询问(n<=10^3,s长度<=10^3)

Input

* Line 1: Two integers: W and N.

* Lines 2..W+1: Line i+1: The ith word in the dictionary.

* Lines W+2..W+N+1: Line W+i+1: A single integer K_i followed by a partial word.

Output

* Lines 1..N: Line i should contain the index within the dictionary (an integer in the range 1..W) of the (K_i)th completion (in alphabetical order) of the ith partial word, or -1 if there are less than K_i completions.

Sample Input

10 3
dab
ba
ab
daa
aa
aaa
aab
abc
ac
dadba
4 a
2 da
4 da

Sample Output

3
1
-1


并没有想到排序加二分。。

直接对所有串建立trie树,维护子树siz,这样我们查询的时候从a开始查然后不断减K即可。

BZ上面加强了数据,直接这么做会T死。

考虑优化:对于Trie树上那些非叶子节点且只有一个儿子的点,可以直接合并到下面的结点上去。。

这个玩意是我受到后缀树的启发bb出来的,复杂度也不会证,应该可以卡掉。

BZ的数据字符总数可能超过1000000。开2100000就够了(我这个做法是这样

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 2100050
int t[N],ch[N][26],W,n,cnt=1,flg[N],fa[N],is[N],vis[N];
char w[N];
void insert(int idx) {
int p=1,i;
for(i=1;w[i];i++) {
int &k=ch[p][w[i]-'a'];
if(!k) k=++cnt;
p=k;
t[p]++;
if(!flg[p]&&!vis[p]) flg[p]=idx;
else if(!is[p]) flg[p]=0,vis[p]=1;
}
is[p]=1;
flg[p]=idx;
}
int query(int p,int k) {
if(k==1&&flg[p]) return flg[p];
int i;
int now=is[p];
for(i=0;i<26;i++) {
now+=t[ch[p][i]];
if(now>=k) {
return query(ch[p][i],k-now+t[ch[p][i]]);
}
}
return flg[p];
}
int main() {
scanf("%d%d",&W,&n);
int i,j,sum=0;
for(i=1;i<=W;i++) {
scanf("%s",w+1);
insert(i);
// sum+=strlen(w+1);
}
// printf("%d\n",sum); return 0;
int K;
for(i=1;i<=n;i++) {
scanf("%d%s",&K,w+1);
int p=1,flg=1;
for(j=1;w[j];j++) {
if(!ch[p][w[j]-'a']) {flg=0; break;}
p=ch[p][w[j]-'a'];
}
if(!flg||t[p]<K) puts("-1");
else printf("%d\n",query(p,K));
}
}

BZOJ_3448_[Usaco2014 Feb]Auto-complete_Trie树的更多相关文章

  1. BZOJ 3446: [Usaco2014 Feb]Cow Decathlon( 状压dp )

    水状压dp. dp(x, s) = max{ dp( x - 1, s - {h} ) } + 奖励(假如拿到的) (h∈s). 时间复杂度O(n * 2^n) ------------------- ...

  2. bzoj3446[Usaco2014 Feb]Cow Decathlon*

    bzoj3446[Usaco2014 Feb]Cow Decathlon 题意: FJ有n头奶牛.FJ提供n种不同的技能供奶牛们学习,每头奶牛只能学习一门技能,每门技能都要有奶牛学习. 第i头奶牛学习 ...

  3. BZOJ3448 : [Usaco2014 Feb]Auto-complete

    RE了几十发,实在没办法了…只好向管理员要数据,然后发现数据规模与题目描述不符… 建立Trie并求出DFS序,同时根据DFS序确定字典序 然后每次询问相当于询问子树第k小,用主席树维护,注意压缩内存 ...

  4. 【BZOJ 3445】【Usaco2014 Feb】Roadblock

    http://www.lydsy.com/JudgeOnline/problem.php?id=3445 加倍的边一定在最短路上(否则继续走最短路). 最短路长度是O(n)的,暴力扫最短路上的每条边, ...

  5. BZOJ 3445: [Usaco2014 Feb] Roadblock

    Description 一个图, \(n\) 个点 \(m\) 条边,求将一条边距离翻倍后使 \(1-n\) 最短路径增加的最大增量. Sol Dijstra. 先跑一边最短路,然后枚举最短路,将路径 ...

  6. [BZOJ3449] [Usaco2014 Feb]Secret Code

    Description Farmer John has secret message that he wants to hide from his cows; the message is a str ...

  7. [Usaco2014 Feb] Roadblock

    有一个无向图,共N个节点,编号1至N,共M条边.FJ在节点1,它想到达节点N.FJ总是会选择最短路径到达节点N .作为捣蛋的奶牛Bessie,它想尽量延迟FJ到达节点N的时间,于是Bessie决定从M ...

  8. BZOJ-USACO被虐记

    bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...

  9. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

随机推荐

  1. 【Ajax 4】Ajax、JavaScript和JQuery的联系和区别

    导读:在之前,就分别学习了Ajax.JavaScript和JQuery,然后对于这三者之间的关系,是一直云里雾里的.尤其是后来学到了Ajax,就更是不明白了.现在,就给总结总结. 一.基本概述 1.1 ...

  2. POJ1690 简单运算去括号

    题目大意: 给定一串只含加减和括号的运算,去掉没用的括号和空白字符输出 这里其实只要去找当前括号前面那个运算符是不是减号,如果是减号且这个括号内出现过运算符说明这个括号应该存在 #include &l ...

  3. 腾讯云CVM使用记录--使用root权限

    1.su root 指令 ,执行下列命令获取root权限: sudo /bin/su - root 注意:严禁执行password命令,root密码默认不能被修改.

  4. 干掉H5audio音频标签的下载按钮

    audio::-internal-media-controls-download-button {display:none;}audio::-webkit-media-controls {overfl ...

  5. Defcon 23最新开源工具NetRipper代码分析与利用

    0×01 研究背景 在分析了俄罗斯人被曝光的几个银行木马的源码后,发现其大多均存在通过劫持浏览器数据包来获取用户个人信息的模块,通过截获浏览器内存中加密前或解密后的数据包来得到数据包的明文数据.在De ...

  6. 【转】Wireshark技巧-过滤规则和显示规则

    原文: http://www.cnblogs.com/icez/p/3973873.html ----------------------------------------------------- ...

  7. 关于Scrum

    最近某些产品经理发出下两周的工作计划的时候,喜欢带上sprint这个字眼,看上去貌似是要走敏捷开发这一套,只可惜,我觉得他表现出来的是对敏捷开发和Scrum一窍不通,甚至对软件开发流程都完全不清楚,居 ...

  8. Python筛选法(算出十亿之内所有的质数之和)

    其实别人写的挺好的了....直接上链接吧http://blog.csdn.net/power721/article/details/8216619

  9. 【Mongodb教程 第十三课 】PHP mongodb 的增删改查使用

    <pre> <?php #phpinfo();die; #其他链接方式 #$conn=new Mongo(); #连接本地主机,默认端口. #$conn=new Mongo(&quo ...

  10. Centos 6.4 搭建LANMP一键安装版

    lanmp一键安装包是wdlinux官网2010年开始推出的lamp,lnmp,lnamp(apache,nginx,php,mysql,zend,eAccelerator,pureftpd)应用环境 ...