哈利波特在魔法学校的必修课之一就是学习魔咒。据说魔法世界有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. python3快捷键

    原文:https://blog.csdn.net/pipisorry/article/details/39909057 PyCharm3.0默认快捷键(翻译的) PyCharm Default Key ...

  2. python面向对象之三大特性

    继承 先看个简单的例子了解一下继承. class Animal: # 父类 def __init__(self, name, age, department): self.name = name se ...

  3. python基础七之集合

    集合:可变的数据类型,他里面的元素必须是不可变的数据类型,无序,不重复. 增加 set1 = {'zxc', 'zxf'} set1.add('zxv') # 无序添加 set1.update('zx ...

  4. java 使用反射调用方法

    每个Method的对象对应一个具体的底层方法.获得Method对象后,程序可以使用Method里面的invoke方法来执行该底层方法. Object invoke(Object obj,Object ...

  5. H3C DNS简介

  6. jquery的offset().top和js的offsetTop的区别,以及jquery的offset().top的实现方法

    jquery的offset().top和js的offsetTop的区别,以及jquery的offset().top的实现方法 offset().top是JQ的方法,需要引入JQ才能使用,它获取的是你绑 ...

  7. TransactionDefinition接口中定义了七个事务传播行为

    1.PROPAGATION_REQUIRED如果存在一个事务,则支持当前事务,如果没有事务则开启一个新的事务.使用spring声明式事务,spring使用AOP来支持声明式事务,会根据事务属性,自动在 ...

  8. The Zen of Python —— Python 之禅

    Beautiful is better than ugly.   # 优美好于丑陋(Python以编写优美的代码为目标) Explicit is better than implicit.   # 明 ...

  9. 修改jupyter notebook响应的浏览器

    Windows下更改jupyter notebook默认响应的浏览器为Chrome 1.命令行下输入:jupyter notebook --generate-config 2.C盘中找到并打开文件:C ...

  10. torch or numpy

    黄色:重点 粉色:不懂 Torch 自称为神经网络界的 Numpy, 因为他能将 torch 产生的 tensor 放在 GPU 中加速运算 (前提是你有合适的 GPU), 就像 Numpy 会把 a ...