题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1880

魔咒词典

Description

哈利波特在魔法学校的必修课之一就是学习魔咒。据说魔法世界有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?

开始用的mp, mle了然后写了个哈希太挫了 掩面(*/ω╲*)。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using std::abs;
using std::sort;
using std::pair;
using std::swap;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 100077;
const int INF = 0x3f3f3f3f;
typedef unsigned long long ull;
struct Hash_Set {
struct edge { char str1[22], str2[82]; int next; }G_1[N], G_2[N];
int tot_1, tot_2, head_1[N], head_2[N];
inline void init() {
tot_1 = 0, cls(head_1, -1);
tot_2 = 0, cls(head_2, -1);
}
inline int hash_func(char *s) {
int val = 0;
while (*s != '\0') {
val = ((val << 1) + (*s - 'a')) % N;
s++;
}
return val;
}
inline void insert_1(char *p, char *p1) {
char *s = p;
int val = hash_func(p);
int u = abs(val) % N;
strcpy(G_1[tot_1].str1, p); strcpy(G_1[tot_1].str2, p1);
G_1[tot_1].next = head_1[u], head_1[u] = tot_1++;
}
inline void insert_2(char *p, char *p1) {
char *s = p;
int val = hash_func(p);
int u = abs(val) % N;
strcpy(G_2[tot_2].str2, p); strcpy(G_2[tot_2].str1, p1);
G_2[tot_2].next = head_2[u], head_2[u] = tot_2++;
}
inline char *find_1(char *p) {
char *s = p;
int val = hash_func(p);
int u = abs(val) % N;
for (int i = head_1[u]; ~i; i = G_1[i].next) {
if (!strcmp(G_1[i].str1, p)) return G_1[i].str2;
}
return NULL;
}
inline char *find_2(char *p) {
char *s = p;
int val = hash_func(p);
int u = abs(val) % N;
for (int i = head_2[u]; ~i; i = G_2[i].next) {
if (!strcmp(G_2[i].str2, p)) return G_2[i].str1;
}
return NULL;
}
}hash;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
char *p;
int n, k;
char buf[200], str1[100], str2[100];
hash.init();
while (gets(buf)) {
if (!strcmp(buf, "@END@")) break;
cls(str2, 0);
p = strchr(buf, ']');
k = p - buf;
strncpy(str1, buf + 1, k - 1), str1[k - 1] = '\0';
strcpy(str2, p + 2);
hash.insert_1(str1, str2), hash.insert_2(str2, str1);
}
scanf("%d", &n);
getchar();
while (n--) {
gets(buf);
if (buf[0] == '[') {
p = strchr(buf, ']');
k = p - buf;
strncpy(str1, buf + 1, k - 1), str1[k - 1] = '\0';
p = hash.find_1(str1);
puts(!p ? "what?" : p);
} else {
p = hash.find_2(buf);
puts(!p ? "what?" : p);
}
}
return 0;
}

hdu 1880 魔咒词典的更多相关文章

  1. HDU 1880 魔咒词典(字符串哈希)

    题目链接 Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一 ...

  2. HDU 1880 魔咒词典 (Hash)

    魔咒词典 Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. hdu 1880 魔咒词典 (字符串哈希)

    魔咒词典 Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. HDU - 1880 魔咒词典~哈希入门

    哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助. 给你一部魔咒词 ...

  5. hdu 1880 魔咒词典(双hash)

    魔咒词典Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. HDU 1880 魔咒词典 (字符串hash)

    <题目链接> 题目大意: 就是每个字符串有一个配套的对应字符串,询问的时候,无论输出其中的哪一个字符串,输出另一个,如果不存在这个字符串,直接输出"what?". 解题 ...

  7. hdu 1880 魔咒字典

    https://vjudge.net/problem/HDU-1880 题意:略 思路: 一开始就是想到了正确的思路,但是代码写炸了,死活过不了.这题嘛,就是建议一个魔咒与咒语的双向映射.首先用字符串 ...

  8. 魔咒词典(hdu 1880)

    Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔 ...

  9. 魔咒词典 HDU - 1880 (字符串hash 单hash转int或者 双hash )

    哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助. 给你一部魔咒词 ...

随机推荐

  1. python拷贝文件到多个文件夹

    主要用来做数据备份,每次用完以后再跑一次脚本,又可以将文件夹下的所有文件拷贝到指定的文件夹内 import os,sys,shutil; class cur_env: path = sys.path[ ...

  2. Swift学习(四)常量&变量&基础数据类型

    常量和变量 常量: 使用let关键词来声明一个常量 所指向的是一个特定类型的值,如数字10或者字符”hello”,常量的值是不能够被二次修改的 编程时使用常量能够让代码看起来更加安全和简洁! let ...

  3. 七、TCP/IP协议

    1.定义: TCP/IP模型也被称作DoD模型(Department of Defense Model).TCP/IP字面上代表了两个协议:TCP(传输控制协议)和IP(网际协议). TCP/IP协议 ...

  4. 在ASP.NET开始执行HTTP请求的处理程序之前

    using Dscf.Client.Web.Class; using Dscf.Client.Web.DscfService; using Dscf.Client.Web.Handler; using ...

  5. Does FTK index search support regular expression?

    Some of my friends ask me a question: "Does FTK index search support regular expression?" ...

  6. Widows2003开机取消按CTRL+ALT+DEL

    一, Widows2003开机取消按CTRL+ALT+DEL 1. 单击windows开始键→管理工具→本地安全策略(如下图) 2. 本地安全设置→本地策略→安全选项 3. 安全选项→右侧→找到这个文 ...

  7. Android开发中Ant命令编译和APK签名的一些心得

    本文章麦子学院跟小伙伴们详细的分享一下关于Android Ant命令行编译和APK签名详解一些实现方法,这是一个朋友在自己做安卓开发时写的,希望对大家会有所帮助呀. 最近在做Android开发时,需要 ...

  8. IOS中获取屏幕尺寸

    //app尺寸,去掉状态栏 CGRect appRect = [UIScreen mainScreen].applicationFrame; NSLog(@"%f, %f, %f,%f&qu ...

  9. [leetcode]_Search Insert Position

    题目:查找元素target插入一个数组中的位置. 代码: public int searchInsert(int[] A, int target) { int len = A.length; int ...

  10. 一款jQuery实现重力弹动模拟效果特效,弹弹弹,弹走IE6

    一款jQuery实现重力弹动模拟效果特效 鼠标经过两块黑色div中间的红色线时,下方的黑快会突然掉落, 并在掉落地上那一刻出现了弹跳的jquery特效效果,非常不错,还兼容所有的浏览器, 适用浏览器: ...