洛谷 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 条双向道路连接,每条路有一 ...
随机推荐
- [BZOJ1088][SCOI2005]扫雷Mine DP
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1088 记录下每一个格子对应左边格子放的雷的情况,然后dp转移就好了. #include&l ...
- Jauery 中Ajax的几种异步请求
以下介绍Jquery中 Post Get Ajax几种异步请求的使用方法 <%@ Page Language="C#" AutoEventWireup=&q ...
- 11 Hash tables
11 Hash tables Many applications require a dynamic set that supports only the dictionary operatio ...
- 迅为I.MX6开发板工业级嵌入式开发平台
迅为-i.MX6开发板是是基于ARM Cortex™-A9架构的高扩展性多核系列应用处理器, i.MX6系列芯片而且根据应用场合的不同,提供了可供选择的单核.双核和四核产品供客户选择.i.MX6系列的 ...
- Oracle中的执行计划
使用autotrace sqlplus系统参数:SQL> set autotrace trace onSQL> select * from dual;DUM---XExecution Pl ...
- Flutter web环境变量搭建及开发
使用flutter开发app已有三个月,有一些行为形成了惯性,在搭建flutter web环境变量时走了不少的坑,分享出来,免得其他小伙伴再走一遍. 首先flutter的版本要使用1.5及以上版本 d ...
- spring的设计思想
在学习Spring框架的时候, 第一件事情就是分析Spring的设计思想 在学习Spring的时候, 需要先了解耦合和解耦的概念 耦合: 简单来说, 在软件工程当中, 耦合是指对象之间的相互依赖 耦合 ...
- SQL server 表操作语句(原创)
CREATE TABLE [dbo].[test] ([id11] int NOT NULL ,[as] varchar(1) COLLATE Chinese_PRC_CI_AS NULL ,[asd ...
- css float属性详解
定义和用法 float 属性定义元素在哪个方向浮动.以往这个属性总应用于图像,使文本围绕在图像周围,不过在 CSS 中,任何元素都可以浮动.浮动元素会生成一个块级框,而不论它本身是何种元素.如果浮动非 ...
- [转]Delphi调用cmd并取得输出文本
//转自http://www.oschina.net/code/snippet_136241_3980 1 procedure CheckResult(b: Boolean); begin if no ...