哈利波特在魔法学校的必修课之一就是学习魔咒。据说魔法世界有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. Spring Boot Thymeleaf 使用详解

    在上篇文章Spring Boot (二):Web 综合开发中简单介绍了一下 Thymeleaf,这篇文章将更加全面详细的介绍 Thymeleaf 的使用.Thymeleaf 是新一代的模板引擎,在 S ...

  2. Vue-axios 在vue cli中封装

    common/post.js import axios from 'axios' // 引入axios import qs from 'qs' // 引入qs axios.defaults.baseU ...

  3. jQuery 工具类函数-检测浏览器是否属于W3C盒子模型

    浏览器的盒子模型分为两类,一类为标准的w3c盒子模型,另一类为IE盒子模型,两者区别为在Width和Height这两个属性值中是否包含padding和border的值,w3c盒子模型不包含,IE盒子模 ...

  4. jQuery 工具类函数-浏览器信息

    在jQuery中,通过$.browser对象可以获取浏览器的名称和版本信息,如$.browser.chrome为true,表示当前为Chrome浏览器,$.browser.mozilla为true,表 ...

  5. 2018.11.23 浪在ACM 集训队第六次测试赛

    2018.11.23 浪在ACM 集训队第六次测试赛 整理人:刘文胜 div 2: A: Jam的计数法 参考博客:[1] 万众 B:数列 参考博客: [1] C:摆花 参考博客: [1] D:文化之 ...

  6. Leecoder466 Count The Repetitons

    Leecoder466 Count The Repetitons 题目大意 定义\([s,n]\)为连续\(n\)个串\(s\)构成的串 现在给定\(s_1,n_1,s_2,n_2\),求最大的\(m ...

  7. vue-learning:3-template-{{}}-and-v-html

    插值{{ }} 和 v-html 本节开始,我们按如下顺序学习vue模板API-指令.点击各部分的DEMO可在线查看代码 输出字符串文本内容的插值:{{}} 输出HMTL元素作为内容的指令:v-htm ...

  8. 使用Sklearn-train_test_split 划分数据集

    使用sklearn.model_selection.train_test_split可以在数据集上随机划分出一定比例的训练集和测试集 1.使用形式为: from sklearn.model_selec ...

  9. JSR-133内存模型手册

    1.介绍 JVM支持多种线程的执行,Threads代表的是线程类,位于java.lang.Thread包下,唯一的方式就是为用户在这个类下的对象创建线程,每一个线程关联着一个对象,一个线程将在star ...

  10. 如何使用JMX来管理程序?

    什么是JMX JMX,全称Java Management Extensions,用于我们管理和监控java应用程序.JMX有以下用途: 监控应用程序的运行状态和相关统计信息. 修改应用程序的配置(无需 ...