HDU 1880 魔咒词典(字符串哈希)
Problem Description
哈利波特在魔法学校的必修课之一就是学习魔咒。据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助。
给你一部魔咒词典。当哈利听到一个魔咒时,你的程序必须告诉他那个魔咒的功能;当哈利需要某个功能但不知道该用什么魔咒时,你的程序要替他找到相应的魔咒。如果他要的魔咒不在词典中,就输出“what?”
Input
首先列出词典中不超过100000条不同的魔咒词条,每条格式为:
[魔咒] 对应功能
其中“魔咒”和“对应功能”分别为长度不超过20和80的字符串,字符串中保证不包含字符“[”和“]”,且“]”和后面的字符串之间有且仅有一个空格。词典最后一行以“@END@”结束,这一行不属于词典中的词条。
词典之后的一行包含正整数N(<=1000),随后是N个测试用例。每个测试用例占一行,或者给出“[魔咒]”,或者给出“对应功能”。
Output
每个测试用例的输出占一行,输出魔咒对应的功能,或者功能对应的魔咒。如果魔咒不在词典中,就输出“what?”
题目大意:略。
思路:字符串哈希。
PS:由于这个题目的所有字符串存起来已经有20+MB,对于这个只有32MB内存限制的题目,用map< stirng, string > 只会MLE,于是只能手写hash。
PS2:主要是为了测试一下pb_ds里的hash。使用方法可以参考WC2015的论文《C++的pb_ds库在OI中的应用》。
- std::map版本(421MS 31536KB)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
typedef long long LL;
const int MAXN = 108;
std::map<int, int> dict_forward, dict_backward;
std::string a[100000], b[100000];
char str1[MAXN], str2[MAXN];
std::string tmp1, tmp2;
int n;
int gethash(const std::string &str) {
LL res = 0;
for(char c : str)
res = (res * 131 + c) % 1000000007;
return res;
}
void myfind(const std::string &s, std::map<int, int> &dict, std::string arr[]) {
auto it = dict.find(gethash(s));
if(it != dict.end()) {
std::cout<<arr[it->second]<<std::endl;
} else {
puts("what?");
}
}
int main() {
int cnt = 0;
while(gets(str1) && strcmp(str1, "@END@") != 0) {
int pos = strchr(str1, ']') - str1;
a[cnt] = std::string(str1 + 1, pos - 1);
b[cnt] = std::string(str1 + pos + 2);
dict_forward[gethash(a[cnt])] = cnt;
dict_backward[gethash(b[cnt])] = cnt;
cnt++;
}
scanf("%d", &n); getchar();
for(int i = 0; i < n; ++i) {
gets(str1);
if(*str1 == '[') {
tmp1 = std::string(str1 + 1, strlen(str1) - 2);
myfind(tmp1, dict_forward, b);
} else {
myfind(str1, dict_backward, a);
}
}
}
- cc_hash_table(265MS 32736KB)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
typedef long long LL;
const int MAXN = 108;
__gnu_pbds::cc_hash_table<int, int> dict_forward, dict_backward;
std::string a[100000], b[100000];
char str[MAXN];
std::string tmp1, tmp2;
int n;
int gethash(const std::string &str) {
LL res = 0;
for(char c : str)
res = (res * 131 + c) % 1000000007;
return res;
}
void myfind(const std::string &s, __gnu_pbds::cc_hash_table<int, int> &dict, std::string arr[]) {
auto it = dict.find(gethash(s));
if(it != dict.end()) {
std::cout<<arr[it->second]<<std::endl;
} else {
puts("what?");
}
}
int main() {
int cnt = 0;
while(gets(str) && strcmp(str, "@END@") != 0) {
int pos = strchr(str, ']') - str;
a[cnt] = std::string(str + 1, pos - 1);
b[cnt] = std::string(str + pos + 2);
dict_forward[gethash(a[cnt])] = cnt;
dict_backward[gethash(b[cnt])] = cnt;
cnt++;
}
scanf("%d", &n); getchar();
for(int i = 0; i < n; ++i) {
gets(str);
if(*str == '[') {
tmp1 = std::string(str + 1, strlen(str) - 2);
myfind(tmp1, dict_forward, b);
} else {
myfind(str, dict_backward, a);
}
}
}
- gp_hash_table(374MS 31216KB)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
typedef long long LL;
const int MAXN = 108;
__gnu_pbds::gp_hash_table<int, int> dict_forward, dict_backward;
std::string a[100000], b[100000];
char str[MAXN];
std::string tmp1, tmp2;
int n;
int gethash(const std::string &str) {
LL res = 0;
for(char c : str)
res = (res * 131 + c) % 1000000007;
return res;
}
void myfind(const std::string &s, __gnu_pbds::gp_hash_table<int, int> &dict, std::string arr[]) {
auto it = dict.find(gethash(s));
if(it != dict.end()) {
std::cout<<arr[it->second]<<std::endl;
} else {
puts("what?");
}
}
int main() {
int cnt = 0;
while(gets(str) && strcmp(str, "@END@") != 0) {
int pos = strchr(str, ']') - str;
a[cnt] = std::string(str + 1, pos - 1);
b[cnt] = std::string(str + pos + 2);
dict_forward[gethash(a[cnt])] = cnt;
dict_backward[gethash(b[cnt])] = cnt;
cnt++;
}
scanf("%d", &n); getchar();
for(int i = 0; i < n; ++i) {
gets(str);
if(*str == '[') {
tmp1 = std::string(str + 1, strlen(str) - 2);
myfind(tmp1, dict_forward, b);
} else {
myfind(str, dict_backward, a);
}
}
}
HDU 1880 魔咒词典(字符串哈希)的更多相关文章
- HDU 1880 魔咒词典 (字符串hash)
<题目链接> 题目大意: 就是每个字符串有一个配套的对应字符串,询问的时候,无论输出其中的哪一个字符串,输出另一个,如果不存在这个字符串,直接输出"what?". 解题 ...
- hdu 1880 魔咒词典
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1880 魔咒词典 Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有10 ...
- hdu 1880 魔咒词典 (字符串哈希)
魔咒词典 Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU - 1880 魔咒词典~哈希入门
哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助. 给你一部魔咒词 ...
- 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 魔咒字典
https://vjudge.net/problem/HDU-1880 题意:略 思路: 一开始就是想到了正确的思路,但是代码写炸了,死活过不了.这题嘛,就是建议一个魔咒与咒语的双向映射.首先用字符串 ...
- 魔咒词典 HDU - 1880 (字符串hash 单hash转int或者 双hash )
哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助. 给你一部魔咒词 ...
- 魔咒词典(hdu 1880)
Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔 ...
随机推荐
- NIO的一些相关链接
Architecture of a Highly Scalable NIO-Based Server Scalable IO in Java Tricks and Tips with NIO part ...
- [超级懒人最简单法]iPhone 6 plus 适配切图方法分享(转载文章)
网络上已经有很多适配教程,可是看了半天总是半懂不懂..最后还是要综合多个教程再动动脑子动动手,最好有程序大哥帮你试一下(这得有多大的福气) 如果有跟我一样情况的: 1. 有人说用sketc ...
- 有限状态机(FSM)
在游戏开发中,AI是个永恒不变的话题,如果你要的AI只是很简单的一个逻辑 那么有限状态机是一个很好的解决方案,尽管在实际开发中,AI的设计并不是一个简单的逻辑, 如果用有限状态机,维护起来会非常麻烦, ...
- 存储过程执行失败与sql668n
某日监控报存储过程执行失败,查看返回码为sql668n [db2inst1@limt bin]$ db2 ? sql668n SQL0668N Operation not allowed for re ...
- python中文注释报错问题
错误信息: SyntaxError: Non-ASCII character '\xe4' in file... 解决办法: 在文件第一行或第二行添加:# -*- coding: utf-8 -*- ...
- Sublime Text 2配置文件详解
Sublime Text 2是那种让人会一眼就爱上的编辑器,不仅GUI让人眼前一亮,功能更是没的说,拓展性目前来说也完全够用了,网上介绍软件的文章和推荐插件的文章也不少,而且很不错,大家可以去找找自己 ...
- C++11 auto and decltype
1.auto关键字 C++新标准引入auto关键词,此auto与之前C语言的auto意义已经不一样了. 这里的auto是修饰未知变量的类型,编译器会通过此变量的初始化自动推导变量的类型. 例如:aut ...
- Swift与OC混编
OC调用Swift的方法:添加 import "xxxx-Swift.h" 头文件即可 Swift调用OC的方法:需要建立桥接: xxxx-Bridging-Header.h 头文 ...
- linux软件包管理(上)
1.二进制包管理(RPM,yum) 2.源代码包的安装 3.脚本安装(shell或java) 4.Debian系列的linux软件包管理简介 在下载rmp包的时候注意检查硬件平台是否正确,如果硬件平台 ...
- PHP 天巡机票接口
一个旅游网站项目,网站需要机票预订接入了天巡机票接口,获取机票信息,不搞不知道,一搞吓一跳比较麻烦. 搜索机票信息需要分2步,首先POST获得一个SESSION,2秒之后,根据这个SESSION,从一 ...