洛谷 P2237 [USACO14FEB]自动完成Auto-complete
题目描述
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.
输入输出样例
说明
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的更多相关文章
- 洛谷 P3102 [USACO14FEB]秘密代码Secret Code 解题报告
P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...
- 洛谷P3102 [USACO14FEB]秘密代码Secret Code
P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...
- 洛谷——P2176 [USACO14FEB]路障Roadblock
P2176 [USACO14FEB]路障Roadblock 题目描述 每天早晨,FJ从家中穿过农场走到牛棚.农场由 N 块农田组成,农田通过 M 条双向道路连接,每条路有一定长度.FJ 的房子在 1 ...
- 洛谷 P3102 [USACO14FEB]秘密代码Secret Code
P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...
- 洛谷 P4343 [SHOI2015]自动刷题机
思路 二分答案 显然的二分答案,但是因为二分判定条件 \(\text{wa}\) 了好几遍-- 可以发现,\(n\) 越大,\(k\) 就越小,所以答案是有单调性的,因此可以用两个二分,一次求最大值, ...
- 洛谷 P3102 [USACO14FEB]秘密代码Secret Code 【区间dp】
农民约翰收到一条的消息,记该消息为长度至少为2,只由大写字母组成的字符串S,他通过一系列操作对S进行加密. 他的操作为,删除S的前面或者后面的若干个字符(但不删光整个S),并将剩下的部分连接到原字符串 ...
- 洛谷 P2176 [USACO14FEB]路障Roadblock
题目描述 每天早晨,FJ从家中穿过农场走到牛棚.农场由 N 块农田组成,农田通过 M 条双向道路连接,每条路有一定长度.FJ 的房子在 1 号田,牛棚在 N 号田.没有两块田被多条道路连接,以适当的路 ...
- 洛谷 2176 [USACO14FEB]路障Roadblock
[题意概述] 修改图中任一一条边的边权,使其加倍,问怎样使修改后图中的1~n的最短路最大.输出最短路的增量. [题解] 先跑一遍dijkstra求出1~n的路径长度,记录下经过的边.枚举这些边进行修改 ...
- 洛谷—— P2176 [USACO14FEB]路障Roadblock
https://www.luogu.org/problem/show?pid=2176 题目描述 每天早晨,FJ从家中穿过农场走到牛棚.农场由 N 块农田组成,农田通过 M 条双向道路连接,每条路有一 ...
随机推荐
- poj3662 Telephone Lines
思路: 二分+最短路.最短路也可以用来计算从a到达b所需的边权不超过x的边的数量. 实现: #include <cstdio> #include <cmath> #includ ...
- Program received signal SIGILL, Illegal instruction
Program received signal SIGILL, Illegal instruction 这个错误,发现是直接在printf 的%s中直接使用string类型,而没有使用c字符串格式造成 ...
- GatewayWorker+Laravel demo
GatewayWorker 结合 Laravel 使用的简单案例,重点是在Laravel中使用GatewayClient发送消息 主要流程:GatewayWorker主要负责推送消息给客户端但不接受客 ...
- 掌握Spark机器学习库-07-最小二乘法
1)最小化残差平方和 2)原理,推导过程 3)例子
- windows ubuntu bcdeditor
双系统. 先装windows,后装ubuntu12.04 为了避免grub引导,所以安装bcdeditor. 平时使用没有什么不适,可是每次linux升级内核以后,grub列表可能就会消失,自然是不能 ...
- Functor、Applicative 和 Monad x
首先,我们来看一下 Functor typeclass 的定义: 1 2 class Functor f where fmap :: (a -> b) -> f a -> f b F ...
- vue动态设置页面title方法
第一种方法 npm install vue-wechat-title --save 在mian.js中引入 //设置title import VueWechatTitle from 'vue-wech ...
- zookeeper、consul 实现注册中心
1.Zookeeper 分布式协调工具,可以实现注册中心 所有实现方式基本一致,只需要先开启zookeeper的服务端,然后再打开客户端jar包即可. Zookeeper一开始连接失败,后面又可以了, ...
- 第2节 hive基本操作:6、7、8
第1节 hive安装:6.hive的基本操作:7.创建数据库的语法:8.hive当中创建内部表的语法. hive的基本操作: 创建数据库与创建数据库表操作 创建数据库操作:create databas ...
- run loop
Objective-C之run loop详解 作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/9237973 转载请注明出 ...