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