单词匹配 - hash
给你一部魔咒词典。当哈利听到一个魔咒时,你的程序必须告诉他那个魔咒的功能;当哈利需要某个功能但不知道该用什么魔咒时,你的程序要替他找到相应的魔咒。如果他要的魔咒不在词典中,就输出“what?”
[魔咒] 对应功能
其中“魔咒”和“对应功能”分别为长度不超过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的更多相关文章
- Ubuntu更新提示哈希和不匹配“Hash Sum mismatch”
Ubuntu更新提示哈希和不匹配"Hash Sum mismatch" 今天在常规更新软件的时候,我的ubuntu报了一个错误. 应该是ubuntu程序更新转交给另外一个更新造成 ...
- 2017阿里C++研发工程师-校招-单词匹配
题目描述 给一个字符串, 然后给一个字典. 把字符串分解成字典里的单词组成的句子, 请输出所需空格最少的方案.并输出该方案. 样例 例如: 字符串为: str="ilikealibaba&q ...
- 290. Word Pattern 单词匹配模式
[抄题]: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
- grep匹配单词, 匹配单词开始, 匹配^ 的区别
grep '^.....$' 是指, 匹配整个这个行中, 以什么开头, 以什么结尾. 指的是整行, 不是某个单词. grep -w (word) 指的是匹配整个单词, 而不能是单词的一部分, 如: g ...
- codevs 3013 单词背诵 hash
题目链接 题目描述 Description 灵梦有n个单词想要背,但她想通过一篇文章中的一段来记住这些单词. 文章由m个单词构成,她想在文章中找出连续的一段,其中包含最多的她想要背的单词(重复的只算一 ...
- 51nod 1095 Anigram单词【hash/map/排序/字典树】
1095 Anigram单词 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 一个单词a如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b ...
- [bzoj3507 Cqoi2014]通配符匹配 (hash+DP)
传送门 Solution 显然用哈希233 设\(f[i][j]\)表示第i个通配符和当前第j个字符是否匹配 考虑两种通配符的特性,直接转移即可 Code #include <cstdio> ...
- 用Hash Table(哈希散列表)实现统计文本每个单词重复次数(频率)
哈希表在查找方面有非常大应用价值,本文记录一下利用哈希散列表来统计文本文件中每个单词出现的重复次数,这个需求当然用NLP技术也很容易实现. 一.基本介绍 1.Hash Key值:将每个单词按照字母组成 ...
- POJ-3267 The Cow Lexicon---删除字符匹配单词
题目链接: https://cn.vjudge.net/problem/POJ-3267 题目大意: 题意就是给出一个主串,和一本字典,问最少在主串删除多少字母,可以使其匹配到字典的单词序列. PS: ...
随机推荐
- 怎么让FOXMAIL关了以后在右下角自动收取邮件
1.缩小到任务栏:打开foxmail,在工具-系统设置-常规,选项中有一项最小化时在任务栏显示,勾选上即可.2.要自动收取邮件,选中邮件账户,右键打开菜单,属性-接收邮件,右边勾选上“每隔*分钟自动收 ...
- 2018-8-10-用-sim-卡加密保护资金
title author date CreateTime categories 用 sim 卡加密保护资金 lindexi 2018-08-10 19:16:52 +0800 2018-2-13 17 ...
- 【mac】关于mac的一些命令
一. 如何查看自己的文件大小,所有文件占了多少? du -h -d 1 . 当前目录文件以及大小 sudo du -h -d 1 / 所目录下的文件以及大小
- vue-learning:0 - 目录
Vue-learning vue.js学习路径 Vue的API地图 点击查看vue的API地图 视图层 点击可直接到达详情页面 指令 {{ }} / v-html v-if / v-else / v- ...
- linux下mysql5.7忘记root密码修改
朋友最近开始学服务器,mysql密码忘了又不会弄,让我帮忙解决一下.重置或修改mysql的root密码这种事平时很少做,还是得google辅助一下,于是弄完了写篇博客记录一下,方便若干月后又有人遇到这 ...
- 2019-8-31-git-上传当前分支
title author date CreateTime categories git 上传当前分支 lindexi 2019-08-31 16:55:59 +0800 2018-05-08 09:2 ...
- Squid使用账号密码进行认证
Squid 3.5支持ssl代理,为保证安全和滥用,可以使用简单的认证. Step1:在squid的配置文件中,添加如下: auth_param basic program /usr/lib64/sq ...
- python字符串(str)
# value = "raitOrEi" # v = value.capitalize()#首字母大写 # print(v) # v1 = v.casefold()#全部变小写,不 ...
- 解析crash
命令行 1.查找 symbolicatecrash find /Applications/Xcode.app -name symbolicatecrash -type f 2.此时会出现一个路径 sy ...
- 使用 postman 给 API 写测试
使用 postman 给 API 写测试 Intro 上次我们简单介绍了 使用 postman 测试 API,这次主要来写一些测试用例以检查请求的响应是否符合我们的预期以及如何使用脚本测试 使用 po ...