魔咒词典 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 // // ...
随机推荐
- linux下libusb的安装与测试
0.libusb的介绍:参考[1] 1.环境:vmware_fedora_10(linux-2.6.x) 2.获取源代码:http://sourceforge.net/projects/libusb/ ...
- jvm内存模型学习心得
昨天面试了两家,备受打击,问的多的就是jvm内存,然额真的是一头雾水.工作中用到的真是少之又少,面试还得问道, 今天恶补了下,在此作以下总结: jvm分为5部分 1.程序计数器 jvm支持多线程运行, ...
- springboot打war包部署tomcat服务器,以及表单提交数据乱码处理
小白觉得springboot打成jar包直接使用内嵌的tomcat或jetty容器(java -jar xxx.jar)运行项目不利于定位问题,我还是习惯于查看tomcat或nginx的日志来定位问题 ...
- redis管道pipeline
Jedis jedis = new Jedis("127.0.0.1",6379); Pipeline pipeline = jedis.pipelined(); for(int ...
- php5.4编译安装apache
1.下载源码包 wget 网址/php-5.4.45.tar2.解压源码包 tar -zxvf php-5.4.45.tar3.创建一个安装目录 mkdir /usr/local/php4.进入解压后 ...
- shims-vue.d.ts 解析
TypeScript的文档看起来比较让人匪夷所思 TS是从2012年就开始的项目,那时ES6的模块化还没有成为继定标准,所以今天来看TS中一些名词让人匪夷所思,其实都是历史遗留问题 比如namespa ...
- 阶段3 1.Mybatis_09.Mybatis的多表操作_4 完成account一对一操作-建立实体类关系的方式
定义user的实体.然后生成getter和setter 定义一个可以封装Account和User的Map type这里虽然是account类型 这一段只能保证account的数据完成.并不能保证use ...
- 思维导图之kubernetes
k8s docker
- 【算法与数据结构】图的最小生成树 MST - Prim 算法
Prim 算法属于贪心算法. #include <stdio.h> #define VERTEXNUM 7 #define INF 10000 typedef struct Graph { ...
- 【MM系列】SAP 关于更改物料的价格控制类型
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 关于更改物料的价格控制类型 ...