
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?
解题思路
哈希映射,因为是字符串映射字符串的,所以容易爆内存
要转换成哈希值的映射
一开始,我的s1,s2是用结构体的,但是它一直output limit
换成2维就过了
还有scanf的正则真好用
代码
#include <iostream>
#include <map>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
const int seed = 7;
const int Max = 1e9+5;
map<int,int> m;
char s1[100005][25];
char s2[100005][85];
int Hash(char* str,int l,int r)
{
int ans=0,t=seed;
for(int i=l;i<=r;i++)
{
if(str[i]!=' ')
{
ans+= (str[i]*t %Max);
t*=t;
t%=Max;
}
}
return (ans%Max);
}
char input[105];
int main()
{
int n=0;
while(1)
{
scanf("%[^\n]",input);getchar();
if(strcmp(input,"@END@")==0) break;
int len = strlen(input);
int l=1;int r=1;
while(input[r]!=']'&&r<len) r++;
memset(s1[n],0,sizeof s1[n]);
strncpy(s1[n],input+1,r-l);
int h = Hash(input,l,r-1);
m[h] = n;
l=r+2;r=len;
memset(s2[n],0,sizeof s2[n]);
strncpy(s2[n],input+l,r-l);
// cout<<s2[n].a<<endl;
h=Hash(input,l,r-1);
m[h]=n;
n++;
}
int N;
scanf("%d",&N);getchar();
while(N--)
{
scanf("%[^\n]",input);getchar();
int h;
int len = strlen(input);
if(input[0]=='[') {
h = Hash(input,1,len-2);
}else{
h = Hash(input,0,len-1);
}
if(m.find(h)==m.end()) cout<<"what?"<<endl;
else{
if(input[0]=='['){
printf("%s\n",s2[m[h]]);
}
else{
printf("%s\n",s1[m[h]]);
}
}
}
return 0;
}
2759

被折叠的 条评论
为什么被折叠?



