单词匹配 - 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: ...
随机推荐
- HDU 6623"Minimal Power of Prime"(数学)
传送门 •题意 给你一个大于 1 的正整数 n: 它可以分解成不同的质因子的幂的乘积的形式,问这些质因子的幂中,最小的幂是多少. •题解 定义 $ans$ 表示最终答案: ①如果 $ans \ge 5 ...
- Python--day32--复习:https和http的区别;黏包;黏包问题的解决方式;
1,https和http的区别: https比较安全,传输的时候先对内容进行加密,收到后再进行解密:它的传输内容不容易拦截,就算拦截下来了,也是加密的,看不懂.但是要买证书,一年要好几万,小公司承担不 ...
- win10 uwp 在 VisualStudio 部署失败,找不到 Windows Phone 可能的原因
在我使用 VisualStudio 调试的时候,发现我插入了手机,但是 VisualStudio 在部署的时候找不到手机. 可能的原因是 手机禁用了连接,第二个原因是可能手机驱动没正确让 Visual ...
- HTTP请求各参数详解
HTTP Request的Header信息 1.HTTP请求方式 如下表: GET 向Web服务器请求一个文件 POST 向Web服务器发送数据让Web服务器进行处理 PUT 向Web服务器发送数据并 ...
- iview table中的render函数使用
1.表格列数据内容过多可以用以下两个属性解决: ellipsis:"true', tooltip:true 使每个列的内容如果过多的话变为省略号 2.table中的render函数(实现根据 ...
- 彻底弄懂slice和splice的区别
总觉得数组和字符串中的一些方法的使用很难记,可能是日常都是在学理论,缺少实际应用.不多说了,继续学习吧! 一句话先提前概括: slice(start,end) 从哪到哪开始删 splice(strt, ...
- luoguP4313 文理分科
luoguP4313 文理分科 复习完之后做了道典型题目. 这道题条件有点多 我们逐个分析 如果没有\(sameart\)或者\(samescience\)的限制,就是一个裸的最大权闭合子图的问题了 ...
- C#面试题整理2(不带答案)
一.C# 理论 1.1.简述 private. protected. public. internal.protected internal 访问修饰符和访问权限 1.2.简述abstract.sea ...
- Qt和c/c++connect函数冲突解决方法
在使用c/c++的connect函数时在前面写::connect()这样就可以解决了
- Unitils集成DBUnit、Spring-单元测试(转)
1.maven-pom文件中引入相关jar包 <!-- Unitils -dbunit.Spring --> <dependency> <groupId>org.u ...