洛谷 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 条双向道路连接,每条路有一 ...
随机推荐
- Android 给按钮添加监听事件
在安卓开发中,如果要给一个按钮添加监听事件的话,有以下三种实现方式 1.方式一 public class MainActivity extends ActionBarActivity { @Overr ...
- 使用Kotlin,抛弃findViewById
有没有觉得Android的findViewById挺烦人的.使用Kotlin可以让你彻底抛弃这个烦恼 步骤1.在build.gradle(Module:app)中添加如下一句话 这个在老一点版本的An ...
- vba,excel,网址提取名字与链接url
'宏操作 Sub 复制超级链接() '这里控制读取A列的第1到10行,你根据自已的要求修改一下起始和结束行数 ).Hyperlinks.Count > ).Value = Cells(a, ). ...
- WindowsService+Quartz.NET快速搭建
新建一个Windows服务项目 nuget安装Quartz.NET,我这边使用的是2.3.3版本 1. Service改名 2. 添加安装程序,改名 3. ServiceInstaller->属 ...
- myslq 5.7 root 默认密码
sudo sumysqld_safe --skip-grant-tables --skip-networking & UPDATE mysql.user SET password=PASSWO ...
- Android(java)学习笔记198:ContentProvider使用之内容观察者(观察发出去的短信)
1.新建一个案例如下: 2. 不需要添加权限,同时这里布局文件不做修改,来到MainActivity,如下: package com.itheima.sendsmslistener; import a ...
- 06CSS列表
CSS列表 列表样式——list-style-type list-style-type:<属性值> disc 黑圆点 circle 空心圆点 square 小黑方块 decimal ...
- OBJECTPROPERTY用法整理
OBJECTPROPERTY用法整理 分类: 系统表与表结构 数据库管理维护2010-06-03 16:37 2783人阅读 评论(1) 收藏 举报 数据库sql serverinsertobject ...
- java枚举中常见的7中用法
2016年08月11日 11:14:45 李学凯 原文链接https://blog.csdn.net/qq_27093465/article/details/52180865 JDK1.5引入了新的 ...
- nginx网站标准配置
#nginx开启的进程数worker_processes 4; #4核CPU #定义全局错误日志定义类型,[debug|info|notice|warn|crit]error_log ...