魔咒词典 HDU - 1880 (字符串hash 单hash转int或者 双hash )
哈利波特在魔法学校的必修课之一就是学习魔咒。据说魔法世界有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?
题意:
思路:
感觉很毒瘤的一题,卡掉map却让暴力可以过,暴力时间复杂度 801e51e3 能过,出题人出数据也长点心。
暴力就太没意思了,讲一下hash的做法吧
把字符串转为ll范围的hash值,然后再用一个map把ll范围的hash值转为 字符串数组的下标即可做。
也可以直接用双hash,就可以用两个int的pair<int,int> 作为字符串的索引,,
有两份代码,第一个是单hash,第二个是双hash。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include<iostream>
#include<string>
#include<map>
#include<utility>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 300007;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
string a[maxn];
// string b[maxn];
const ll p = 6151ll;
const ll mod = 1610612741;
map<ll, int> m;
string s1;
int main() {
// freopen("D:\\code\\text\\input.txt", "r", stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
// gbtb;
string s2;
int id = 0;
while (1) {
getline(cin, s1);
if (s1[0] == '@') {
break;
}
s2 = s1.substr(s1.find(']') + 2);
s1 = s1.substr(0, s1.find(']'));
s1.erase(s1.begin());
int len = s1.length();
// cout << s1 << endl << s2 << endl;
ll num = 0ll;
repd(i, 0, len - 1) {
num = (num * p % mod + s1[i]) % mod;
}
// cout << num << " " << s1 << endl;
a[++id] = s2;
m[num] = id;
num = 0ll;
len = s2.length();
repd(i, 0, len - 1) {
num = (num * p % mod + s2[i]) % mod;
}
// cout << num << " " << s2 << endl;
a[++id] = s1;
m[num] = id;
// cout<<s1<<" "<<s2<<endl;
}
int q;
// cin >> q;
scanf("%d", &q);
string ask;
getchar();
// getline(cin, ask);
while (q--) {
getline(cin, ask);
// chu(ask);
if (ask[0] == '[') {
int len = ask.length();
ll num = 0ll;
repd(i, 1, len - 2) {
num = (num * p % mod + ask[i]) % mod;
}
// cout << num << " " << ask << endl;
if (m.find(num) == m.end()) {
cout << "what?" << '\n';
} else {
cout << a[m[num]] << "\n";
}
} else {
int len = ask.length();
ll num = 0ll;
repd(i, 0, len - 1) {
num = (num * p % mod + ask[i]) % mod;
}
// cout << num << " " << ask << endl;
if (m.find(num) == m.end()) {
cout << "what?" << '\n';
} else {
cout << a[m[num]] << "\n";
}
}
}
// cout << endl;
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
双hash
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN=1e5+7; //同时充当mod作用
const int Hash_1=131, Hash_2=233;
int Q;
map<pair<int, int>, string> mp;
char is[300];
char a[110], b[110];
void init()
{
mp.clear();
}
int get1(char *s)
{
int ans=0;
for(int i=0; s[i]; i++) { ans=( ans*Hash_1%maxN + s[i] )%maxN; }
return ans;
}
int get2(char *s)
{
int ans=0;
for(int i=0; s[i]; i++) { ans=( ans*Hash_2%maxN + s[i] )%maxN; }
return ans;
}
int main()
{
init();
while(gets(is) && is[0]!='@')
{
int len=(int)strlen(is);
int i=1, j=0, tmp1=0, tmp2=0, tmp3=0, tmp4=0;
for(i=1; is[i]!=']'; i++) a[i-1]=is[i];
a[i-1]=0;
tmp1=get1(a);
tmp2=get2(a);
i+=2;
for(j=i; j<len; j++) b[j-i]=is[j];
b[j-i]=0;
tmp3=get1(b);
tmp4=get2(b);
mp[make_pair(tmp1, tmp2)]=b;
mp[make_pair(tmp3, tmp4)]=a;
//getchar();
}
scanf("%d", &Q);
getchar();
while(Q--)
{
//scanf("%s", is);
gets(is);
if(is[0]=='[')
{
int len=(int)strlen(is);
is[len-1]=0;
int tmp1=0, tmp2=0;
tmp1=get1(is+1);
tmp2=get2(is+1);
if(mp.find(make_pair(tmp1, tmp2))!=mp.end()) cout<<mp[make_pair(tmp1, tmp2)]<<endl;
else printf("what?\n");
}
else
{
int len=(int)strlen(is);
is[len]=0;
int tmp1=0, tmp2=0;
tmp1=get1(is);
tmp2=get2(is);
if(mp.find(make_pair(tmp1, tmp2))!=mp.end()) cout<<mp[make_pair(tmp1, tmp2)]<<endl;
else printf("what?\n");
}
//getchar();
}
return 0;
}
魔咒词典 HDU - 1880 (字符串hash 单hash转int或者 双hash )的更多相关文章
- HDU 1880 魔咒词典(字符串哈希)
题目链接 Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一 ...
- HDU 1880 魔咒词典 (Hash)
魔咒词典 Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 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 Submiss ...
- hdu 1880 魔咒词典
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1880 魔咒词典 Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有10 ...
- HDU - 1880 魔咒词典~哈希入门
哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助. 给你一部魔咒词 ...
- 魔咒词典(hdu 1880)
Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔 ...
- HDU 1880 字符串hash 入门题
Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔 ...
- 题目1029:魔咒词典(map使用以及字符串读取函数总结)
题目链接:http://ac.jobdu.com/problem.php?pid=1029 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus // // ...
随机推荐
- 第十周java学习总结
目录 第十周java学习总结 学习内容 代码上传截图 代码链接 第十周java学习总结 学习内容 第12章 Java多线程机制 主要内容 Java中的线程 Thread类与线程的创建 线程的常用方法 ...
- SecureCRT通过密钥登录
转载 https://blog.csdn.net/langkeziju/article/details/53024031 说明:一般的密码方式登录容易被密码暴力破解.所以一般我们会将 SSH 的端口 ...
- leetcode-mid-sorting and searching-162. Find Peak Element
mycode 54.81% class Solution(object): def findPeakElement(self, nums): """ :type num ...
- net.sf.json和com.alibaba.fastjson两种json加工类的相关使用方法
com.alibaba.fastjson Fastjson是一个Java语言编写的高性能功能完善的JSON库.它采用一种“假定有序快速匹配”的算法,把JSON Parse的性能提升到极致,是目前Jav ...
- ThreadLocal 源码分析
线程局部变量 ThreadLocal 用于实现线程隔离和类间变量共享. 创建实例 /** * 当前 ThreadLocal 实例的哈希值 */ private final int threadLoca ...
- 将ubuntu系统录到u盘上
可以使用bootice工具对u盘分区,并隐藏,然后把系统录到隐藏分区,ubuntu只需要1.5G即可.bootice很强大 录制工具可选的有Universal USB Install.UltraISO ...
- Python学习之==>第三方模块的安装、模块导入
一.模块&包 1.模块 模块实质上就是一个Python文件,它是用来组织代码的.意思就是把Python代码写在里面,文件名就是模块的名称.例如:random.py,random就是模块的名称. ...
- map根据属性排序、取出map前n个
/** * map根据value排序 * flag = 1 正序 * flag = 0 倒序 * * @param map * @param flag * @return */ public stat ...
- mysql 锁超时
对一个别人正在读写的表执行DDL操作,经常需要先锁表,但是这个表正在被人执行读写操作,那么就会报:Lock wait timeout 类的错误. 通过MDB实例详情页面的进程管理可以看到类似如下的情况 ...
- 【MM系列】SAP 的账期分析和操作
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 的账期分析和操作 前言部 ...