HDU - 1880 魔咒词典~哈希入门
给你一部魔咒词典。当哈利听到一个魔咒时,你的程序必须告诉他那个魔咒的功能;当哈利需要某个功能但不知道该用什么魔咒时,你的程序要替他找到相应的魔咒。如果他要的魔咒不在词典中,就输出“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? 第一眼看这题 就感觉用map暴力应该OK
结果爆内存了
还是老老实实用hash吧
hash其实就是把字符串映射为一个特有的数字然后通过查询这个特有的数字进行字符串查找 这题还让我学会了如何对结构体使用lower——bound 这题收获还是比较多的
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string> using namespace std;
const int maxn = 1e5 + ;
char a[maxn][], b[maxn][];
int num = ;
struct node {
int Hash;
int id;
} key[maxn], tail[maxn] ;
int cmp(node a, node b) {
return a.Hash < b.Hash;
}
int bkdrhash(char *str) {
int seed = ;
int ret = ;
while(*str)
ret = ret * seed + (*str++);
return ret & 0x7fffffff;
}
void init() {
for (int i = ; i < num ; i++) {
key[i].Hash = bkdrhash(a[i]);
key[i].id = i;
tail[i].Hash = bkdrhash(b[i]);
tail[i].id = i;
}
sort(key, key + num, cmp);
sort(tail, tail + num, cmp);
} int main() {
while(scanf("%s", a[num])) {
if (a[num][] == '@') break;
getchar();
gets(b[num++]);
}
init();
int n;
scanf("%d", &n);
getchar();
for (int i = ; i < n ; i++ ) {
node temp;
char str[];
gets(str);
if (str[] == '[') {
temp.Hash = bkdrhash(str);
int mid = lower_bound(key, key + num, temp, cmp) - key;
if (key[mid].Hash == temp.Hash) printf("%s\n", b[key[mid].id]);
else printf("what?\n");
} else {
temp.Hash = bkdrhash(str);
int mid = lower_bound(tail, tail + num, temp, cmp) - tail;
if (tail[mid].Hash == temp.Hash) {
int len = strlen(a[tail[mid].id]);
a[tail[mid].id][len - ] = ;
printf("%s\n", a[tail[mid].id] + );
} else printf("what?\n");
}
}
return ;
}
HDU - 1880 魔咒词典~哈希入门的更多相关文章
- hdu 1880 魔咒词典
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1880 魔咒词典 Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有10 ...
- HDU 1880 魔咒词典(字符串哈希)
题目链接 Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一 ...
- hdu 1880 魔咒词典 (字符串哈希)
魔咒词典 Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 1880 魔咒词典 (Hash)
魔咒词典 Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 1880 魔咒词典(双hash)
魔咒词典Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1880 魔咒词典 (字符串hash)
<题目链接> 题目大意: 就是每个字符串有一个配套的对应字符串,询问的时候,无论输出其中的哪一个字符串,输出另一个,如果不存在这个字符串,直接输出"what?". 解题 ...
- hdu 1880 魔咒字典
https://vjudge.net/problem/HDU-1880 题意:略 思路: 一开始就是想到了正确的思路,但是代码写炸了,死活过不了.这题嘛,就是建议一个魔咒与咒语的双向映射.首先用字符串 ...
- 魔咒词典(hdu 1880)
Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔 ...
- 魔咒词典 HDU - 1880 (字符串hash 单hash转int或者 双hash )
哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助. 给你一部魔咒词 ...
随机推荐
- SGU 495
#include<bits/stdc++.h> using namespace std; #define ll long long ; ; int n,m; double dp[N]; / ...
- WPF中使用第三方字体选择器
原文:WPF中使用第三方字体选择器 起因 到WPF的字体可以设置的东西变得非常的多,而却没有提供专用的字体选择对话框,甚至于WinFrom的FontDialog也是不能直接用来设置WPF中的字体.解决 ...
- 初步学习pg_control文件之十五
接前文 初步学习pg_control文件之十四 再看如下这个: int MaxConnections; 应该说,它是一个参考值,在global.c中有如下定义 /* * Primary determ ...
- 插件开发遇到的坑------final 型变量,编译过程被优化
android 插件开发遇到的坑 今天遇到一个坑,pdf 插件,调用了主工程的一个静态final 字符串,但是主工程里面已经没有这个字符串了,却没有崩溃. 后来同事说,因为字符串可能已经直接被写死了. ...
- 【jQuery】 js 对象
[jQuery] js 对象 一. 创建对象的三种方式 <script> var v1 = new Object(); v1.name = "name1"; v1.a ...
- 各种网站,app的手机号绑定真坑爹
各种网站,app的手机号绑定真坑爹,无力吐槽,哎
- 自动化测试学习之路--java String、StringBuilder
Java中的String和StringBuilder类: 1.String对象是不可变的.每一个看起来修改了String值的方法,实际上都是创建了全新的String对象.代码示例如下: String ...
- spring+apache dbcp +oracle 连接池配置以及优化
特此记录 <!-- 数据源配置, 使用应用中的DBCP数据库连接池 --> <bean id="dataSource" class="org.apach ...
- 学习人工智能的第五个月[字典学习[Dictionary Learning,DL]]
摘要: 大白话解释字典学习,分享第五个月的学习过程,人生感悟,最后是自问自答. 目录: 1.字典学习(Dictionary Learning,DL) 2.学习过程 3.自问自答 内容: 1.字典学习( ...
- Visual Studio 2014安装包
点击下载