题目描述

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. 11 DOM基础

    1.css   标签 js       元素 dom    节点,元素节点,属性节点,文本节点 2.dom浏览器支持率 ie      10% chrome   60% FF       99% 3. ...

  2. orcale 数据库的一些知识

    最近学了一些Oracle数据库的知识,我想自己整理一下,以后也方便自己查阅的. orcale 数据库登录(tiger) 1. sql plus 登录 用户名: sys 口令: 主机字符串:orcl a ...

  3. php查询快递信息

    $code = 'shunfeng'; $invoice = '952255884068'; $test = getExpressDelivery($code,$invoice); function ...

  4. mysql中判断条件

    if / case when 判断 SELECT CASE 1 WHEN 1 THEN "one" WHEN 2 THEN "two" ELSE "m ...

  5. Katalon Studio(二) 进阶战の Jenkins集成 analytics.katalon 集成

    本教程只针对Katalon Studio 与CI工具之一Jenkins的集成与脚本集的测试报告可视化简单操作. 1.新建一个job 2.新建一个自由风格的job 3.构建触发器 4.构建Windows ...

  6. Farseer.net轻量级ORM开源框架 V1.x 入门篇:数据库配置文件

    导航 目   录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:新版本说明 下一篇:Farseer.net轻量级ORM开源框架 ...

  7. Ghost Win10系统X64位和32位10041装机版下载

    更多系统下载尽在系统妈:http://www.xitongma.com 特别说明: 1.C:盘分区须至少15GB(安装过程有大量的解压临时文件),安装完成后C:盘占用10GB左右! 2.安装之后如有硬 ...

  8. vue动态设置页面title方法

    第一种方法 npm install vue-wechat-title --save 在mian.js中引入 //设置title import VueWechatTitle from 'vue-wech ...

  9. 多线程之Java中的等待唤醒机制

    多线程的问题中的经典问题是生产者和消费者的问题,就是如何让线程有序的进行执行,获取CPU执行时间片的过程是随机的,如何能够让线程有序的进行,Java中提供了等待唤醒机制很好的解决了这个问题! 生产者消 ...

  10. react之webpack

    1. 下载相关模块包 * 创建package.json ``` npm init ``` * react相关库 package-lock.json ``` npm install react reac ...