题目描述

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.

贝西新买了手机,打字不方便,请设计一款应用,帮助她快速发消息。

字典里有W(W<=30000)个小写字母构成的单词,所有单词的字符总数量不超过1,000,000,这些单词是无序的。现在给出N(1 <= N <= 1000)个询问,每个询问i包含一个的字符串s_i(每个字符串最多包含1000个字符)和一个整数K_i,对于所有以s_i为前缀的单词,其中按字典序排序后的第K_i个单词,求该单词在原字典里的序号。

输入输出格式

输入格式:

* 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.

输出格式:

* 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.

输入输出样例

输入样例#1: 复制

10 3
dab
ba
ab
daa
aa
aaa
aab
abc
ac
dadba
4 a
2 da
4 da
输出样例#1: 复制

3
1
-1

说明

OUTPUT DETAILS:

The completions of a are {aa,aaa,aab,ab,abc,ac}. The 4th is ab, which

is listed on line 3 of the dictionary. The completions of da are

{daa,dab,dadba}. The 2nd is dab, listed on line 1 of the dictionary.

There is no 4th completion of da.

Source: USACO 2014 Feburary Contest, Silver

#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
map<string,int>ma;
int n,m;
string s[];
bool judge(string ss,string p){
int lenp=p.length();
for(int i=;i<lenp;i++)
if(ss[i]!=p[i]) return false;
return true;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
cin>>s[i];
ma[s[i]]=i;
}
sort(s+,s++n);
for(int i=;i<=m;i++){
int k;string p;
scanf("%d",&k);cin>>p;
int now=lower_bound(s+,s++n,p)-s;
if(judge(s[now+k-],p)) printf("%d\n",ma[s[now+k-]]);
else printf("-1\n");
}
}

二分+排序

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,k,tot,f;
string s,p;
int sum[],id[];
int trie[][];
void insert(int now){
int len=s.length();
int root=;
for(int i=;i<len;i++){
int x=s[i]-'a';
if(!trie[root][x]) trie[root][x]=++tot;
sum[trie[root][x]]++;
root=trie[root][x];
}
id[root]=now;
}
void dfs(int now){
if(k==&&id[now]!=){
cout<<id[now]<<endl;f=;
return ;
}
for(int i=;i<;i++){
if(f==) continue;
if(sum[trie[now][i]]>=k){
if(id[trie[now][i]]!=&&k!=) k--;
dfs(trie[now][i]);
}
else k-=sum[trie[now][i]];
}
}
void find(){
int len=p.length();
int root=;f=;
for(int i=;i<len;i++){
int x=p[i]-'a';
if(!trie[root][x]){
cout<<"-1"<<endl;
return ;
}
root=trie[root][x];
}
if(sum[root]<k){
cout<<"-1"<<endl;
return ;
}
if(id[root]) k--;
dfs(root);
}
int main(){
//freopen("lpp1.in","r",stdin);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
cin>>s;
insert(i);
}
for(int i=;i<=m;i++){
cin>>k;cin>>p;
find();
}
}

trie树

洛谷 P2237 [USACO14FEB]自动完成Auto-complete的更多相关文章

  1. 洛谷 P3102 [USACO14FEB]秘密代码Secret Code 解题报告

    P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...

  2. 洛谷P3102 [USACO14FEB]秘密代码Secret Code

    P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...

  3. 洛谷——P2176 [USACO14FEB]路障Roadblock

    P2176 [USACO14FEB]路障Roadblock 题目描述 每天早晨,FJ从家中穿过农场走到牛棚.农场由 N 块农田组成,农田通过 M 条双向道路连接,每条路有一定长度.FJ 的房子在 1 ...

  4. 洛谷 P3102 [USACO14FEB]秘密代码Secret Code

    P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...

  5. 洛谷 P4343 [SHOI2015]自动刷题机

    思路 二分答案 显然的二分答案,但是因为二分判定条件 \(\text{wa}\) 了好几遍-- 可以发现,\(n\) 越大,\(k\) 就越小,所以答案是有单调性的,因此可以用两个二分,一次求最大值, ...

  6. 洛谷 P3102 [USACO14FEB]秘密代码Secret Code 【区间dp】

    农民约翰收到一条的消息,记该消息为长度至少为2,只由大写字母组成的字符串S,他通过一系列操作对S进行加密. 他的操作为,删除S的前面或者后面的若干个字符(但不删光整个S),并将剩下的部分连接到原字符串 ...

  7. 洛谷 P2176 [USACO14FEB]路障Roadblock

    题目描述 每天早晨,FJ从家中穿过农场走到牛棚.农场由 N 块农田组成,农田通过 M 条双向道路连接,每条路有一定长度.FJ 的房子在 1 号田,牛棚在 N 号田.没有两块田被多条道路连接,以适当的路 ...

  8. 洛谷 2176 [USACO14FEB]路障Roadblock

    [题意概述] 修改图中任一一条边的边权,使其加倍,问怎样使修改后图中的1~n的最短路最大.输出最短路的增量. [题解] 先跑一遍dijkstra求出1~n的路径长度,记录下经过的边.枚举这些边进行修改 ...

  9. 洛谷—— P2176 [USACO14FEB]路障Roadblock

    https://www.luogu.org/problem/show?pid=2176 题目描述 每天早晨,FJ从家中穿过农场走到牛棚.农场由 N 块农田组成,农田通过 M 条双向道路连接,每条路有一 ...

随机推荐

  1. 分享div、text、Box Shadow(阴影)演示及代码的页面

    附图: 直接上链接:www.css88.com/tool/css3Preview/Box-Shadow.html

  2. C# 移动开发 MasterDetailPage 侧滑

    先上结果图: 虽然是跨平台的安卓和ios都可以运行,由于目前只配置了安卓的,ios的先不理. 我们先新建一个项目,跨平台应用: 可移植类库: 可移植项目右键添加新建项 选 Forms MasterDe ...

  3. python学习日记-01

    一. 熟悉 在正式介绍python之前,了解下面两个基本操作对后面的学习是有好处的: (1)基本的输入输出 可以在Python中使用+.-.*./直接进行四则运算. >>> 1+3* ...

  4. CentOS6.8 RPM包安装快速zabbix22

    CentOS6.8 RPM包安装快速zabbix22 yum install -y epel-release # yum install -y httpd php php-devel mysql-se ...

  5. Filter简介

    Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 htm ...

  6. ORACLE in与exists语句的区别(一)

    select * from Awhere id in(select id from B) 以上查询使用了in语句,in()只执行一次,它查出B表中的所有id字段并缓存起来.之后,检查A表的id是否与B ...

  7. MFC模拟鼠标点击

    MFC 工程 把以下代码放到你想要响应的函数里面就行 CPoint pt; GetCursorPos(&pt);//获取鼠标在屏幕的当前位置 SetCursorPos(100,200);//移 ...

  8. Python使用Flask框架,结合Highchart,自定义图表样式主题

    参考链接:https://www.highcharts.com.cn/docs/themes 1.使用官方提供的主题js文件,只需要在 highcharts.js 后引入对应的文件即可,不用修改原有的 ...

  9. MYSQL有那些优化?

    版权声明:本文为博主转载文章,原博主地址: https://blog.csdn.net/u013087513/article/details/77899412 MySQL优化三大方向 ① 优化MySQ ...

  10. 树莓派 - wiringPi

    wiringPi其实和BCM2835 library类似,也是通过memmap, IOmap来实现在用户空间直接操作底层寄存器 wiringPi http://wiringpi.com/ Wiring ...