哈利波特在魔法学校的必修课之一就是学习魔咒。据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助。

给你一部魔咒词典。当哈利听到一个魔咒时,你的程序必须告诉他那个魔咒的功能;当哈利需要某个功能但不知道该用什么魔咒时,你的程序要替他找到相应的魔咒。如果他要的魔咒不在词典中,就输出“what?”

Input首先列出词典中不超过100000条不同的魔咒词条,每条格式为:

[魔咒] 对应功能

其中“魔咒”和“对应功能”分别为长度不超过20和80的字符串,字符串中保证不包含字符“[”和“]”,且“]”和后面的字符串之间有且仅有一个空格。词典最后一行以“@END@”结束,这一行不属于词典中的词条。

词典之后的一行包含正整数N(<=1000),随后是N个测试用例。每个测试用例占一行,或者给出“[魔咒]”,或者给出“对应功能”。Output每个测试用例的输出占一行,输出魔咒对应的功能,或者功能对应的魔咒。如果魔咒不在词典中,就输出“what?”Sample Input

[expelliarmus] the disarming charm
[rictusempra] send a jet of silver light to hit the enemy
[tarantallegra] control the movement of one's legs
[serpensortia] shoot a snake out of the end of one's wand
[lumos] light the wand
[obliviate] the memory charm
[expecto patronum] send a Patronus to the dementors
[accio] the summoning charm
@END@
4
[lumos]
the summoning charm
[arha]
take me to the sky

Sample Output

light the wand
accio
what?
what? 题意 : 给你一个咒语对应着一个功能,要求有 q 此询问,对于每次询问给出相应的咒语后告诉你相应的功能
思路分析:这题用 map 可能会超内存,因此我们这里用 hash 去做,对于 hash 后的值相同的情况下我们去连一条链,然后匹配的时候去在这条链上去匹配即可。
代码示例:
using namespace std;
#define ll unsigned long long
const ll maxn = 1e6+5; char s[200];
struct node
{
char que[25];
char ans[85];
int next; }a[maxn], b[maxn];
ll p = 100007; char que_[25], ans_[85];
int ha[maxn], hb[maxn];
ll cura=0, curb=0; ll gethash(char *str){
ll res = 0;
for(ll i = 0; *(str+i); i++){
res = res*p+(str[i]-'a');
}
return res%p;
} void insert(){
ll hash_a = gethash(que_);
strcpy(a[cura].que, que_);
strcpy(a[cura].ans, ans_);
a[cura].next = ha[hash_a];
ha[hash_a] = cura;
cura++; ll hash_b = gethash(ans_);
strcpy(b[curb].que, que_);
strcpy(b[curb].ans, ans_);
b[curb].next = hb[hash_b];
hb[hash_b] = curb;
curb++; //printf("++++ %llu %llu \n", hash_a, hash_b);
} bool searcha(char *str){
ll num = gethash(str);
int x= ha[num]; //printf("1111111111 %llu %d\n", num, x);
while(x != -1){
//printf("_______ %s \n", a[x].que);
if (strcmp(a[x].que, str) == 0){
printf("%s\n", a[x].ans);
return true;
}
x = a[x].next;
}
return false;
} bool searchb(char *str){
ll num = gethash(str);
int x= hb[num];
//printf("2222222222 %llu %llu\n", num, x); while(x != -1){ if (strcmp(b[x].ans, str) == 0){
printf("%s\n", b[x].que);
return true;
}
x = b[x].next;
}
return false;
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
memset(ha, -1, sizeof(ha));
memset(hb, -1, sizeof(hb));
while(1){
gets(s+1);
ll len = strlen(s+1);
ll pos = 1;
if (s[1] == '@') break;
ll k = 0;
for(ll i = 2; i <= len; i++){
if (s[i] == ']') {pos = i; break;}
que_[k++] = s[i];
}
que_[k] = '\0';
k = 0;
for(ll i = pos+2; i <= len; i++){
ans_[k++] = s[i];
}
ans_[k] = '\0';
insert();
}
ll q; cin >>q;
getchar();
while(q--){
gets(s);
ll len = strlen(s);
if (s[0] == '['){
s[len-1] = '\0';
s[0] = '\0';
if (!searcha(s+1)) printf("what?\n");
}
else {
if (!searchb(s)) printf("what?\n");
}
}
return 0;
}

单词匹配 - hash的更多相关文章

  1. Ubuntu更新提示哈希和不匹配“Hash Sum mismatch”

    Ubuntu更新提示哈希和不匹配"Hash Sum mismatch" 今天在常规更新软件的时候,我的ubuntu报了一个错误. 应该是ubuntu程序更新转交给另外一个更新造成 ...

  2. 2017阿里C++研发工程师-校招-单词匹配

    题目描述 给一个字符串, 然后给一个字典. 把字符串分解成字典里的单词组成的句子, 请输出所需空格最少的方案.并输出该方案. 样例 例如: 字符串为: str="ilikealibaba&q ...

  3. 290. Word Pattern 单词匹配模式

    [抄题]: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...

  4. grep匹配单词, 匹配单词开始, 匹配^ 的区别

    grep '^.....$' 是指, 匹配整个这个行中, 以什么开头, 以什么结尾. 指的是整行, 不是某个单词. grep -w (word) 指的是匹配整个单词, 而不能是单词的一部分, 如: g ...

  5. codevs 3013 单词背诵 hash

    题目链接 题目描述 Description 灵梦有n个单词想要背,但她想通过一篇文章中的一段来记住这些单词. 文章由m个单词构成,她想在文章中找出连续的一段,其中包含最多的她想要背的单词(重复的只算一 ...

  6. 51nod 1095 Anigram单词【hash/map/排序/字典树】

    1095 Anigram单词 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 一个单词a如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b ...

  7. [bzoj3507 Cqoi2014]通配符匹配 (hash+DP)

    传送门 Solution 显然用哈希233 设\(f[i][j]\)表示第i个通配符和当前第j个字符是否匹配 考虑两种通配符的特性,直接转移即可 Code #include <cstdio> ...

  8. 用Hash Table(哈希散列表)实现统计文本每个单词重复次数(频率)

    哈希表在查找方面有非常大应用价值,本文记录一下利用哈希散列表来统计文本文件中每个单词出现的重复次数,这个需求当然用NLP技术也很容易实现. 一.基本介绍 1.Hash Key值:将每个单词按照字母组成 ...

  9. POJ-3267 The Cow Lexicon---删除字符匹配单词

    题目链接: https://cn.vjudge.net/problem/POJ-3267 题目大意: 题意就是给出一个主串,和一本字典,问最少在主串删除多少字母,可以使其匹配到字典的单词序列. PS: ...

随机推荐

  1. Navicat for MySQL 使用SSH方式链接远程数据库(二)

    这里我们使用SSH连接远程mysql数据库 2 SSH这种方式,可以使我们连接到远程服务器,但是现在并不能访问数据库,因为我们还没有连接到数据库 3 既然已经连接到服务器了,我们就该连接服务器上的数据 ...

  2. Python--day40--threading模块

    import time from threading import Thread class MyThread(Thread): def __init__(self,arg): super().__i ...

  3. 深入理解CSS盒模型(转)

    转自:https://www.cnblogs.com/chengzp/p/cssbox.html 基本概念 盒模型的组成大家肯定都懂,由里向外content,padding,border,margin ...

  4. C# 如何给 ValueTuple 返回值添加注释

    在 C# 7.0 可以在一个方法的返回,返回多个参数,通过 ValueTuple 的方法,但是和单个参数返回不同的是,如何对多个参数返回每个参数进行单独的注释? 如使用下面的代码,我尝试在一个方法返回 ...

  5. CF1151FSonya and Informatics

    CF1151FSonya and Informatics 给一个长度为 n$ (n\leq 100)$的 \(0/1\) 串,进行 k\((k \leq 10^9)\)次操作,每次操作选择两个位置 \ ...

  6. poj2826 An Easy Problem?!(计算几何)

    传送门 •题意 两根木块组成一个槽,给定两个木块的两个端点 雨水竖直下落,问槽里能装多少雨水, •思路 找不能收集到雨水的情况 我们令线段较高的点为s点,较低的点为e点 ①两条木块没有交点 ②平行或重 ...

  7. php 上传文件并对上传的文件进行简单验证(错误信息,格式(防伪装),大小,是否为http上传)

    <body> <?php /** *验证错误 *如果有错,就返回错误,如果没错,就返回null */ function check($file) { //1:验证是否有误 if($f ...

  8. Python1_Python的目录结构、执行顺序、__name__ == __main__

    Python执行顺序 python属于脚本语言,不像编译型的语言那样先将程序编译成二进制后再运行,而是动态地逐行解释运行: 也就是从脚本的第一行开始运行,没有统一的入口. python会从文件的第一行 ...

  9. slim中返回结果加密的

    //返回结果不加密 $this->get("/open]",function (Request $request, Response $response, $args) {  ...

  10. 还在拼字符串?试试HTML5的template标签

    HTML5中<template>标签的详细介绍(图文) 这篇文章主要介绍了HTML5中的template标签,是HTML5入门中的重要知识,需要的朋友可以参考 一.HTML5 templa ...