题目链接
这道题更多的感觉是在考你对于字符类型的处理,用了scanf("%s")不行,然后转用gets()然后就可以正常输入输出了,最后有一件很神奇的事,不要用G++,有毒:
同样的代码,我用了C++交就过了。
#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;
}
get1()得到的是第一重哈希,get2()得到的是第二重哈希,我们用双重哈希来得到精确的哈希值答案。