题意:一些结构体,键和值是字符串,有若干组询问,输入键,输出值。
分析:写一个递归函数,用把每一个键对应一个哈希值,对应的值为开始和结束下标。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
char s[3000000];
char str[3000000];
ull p=17;
ull mod=1000000000000007;
map<ull,pii>mp;
int b;
int idx(char ch)
{
if(ch>='0'&&ch<='9')
return ch-'0';
else if(ch>='a'&&ch<='z')
return ch-'a'+10;
else if(ch>='A'&&ch<='Z')
return ch-'A'+36;
else if(ch=='.')
return 62;
else
return 63;
}
void dfs(ull u)
{
ull ans=u;
while(s[b]!='}')
{
if(s[b]==':')
{
if(s[b+1]=='{')
{
int a=b+1;
b+=2;
dfs((ans*p+62)%mod);
mp[ans]=pii(a,b);
b++;
if(s[b]=='}')
break;
else
b++;
}
else
{
int a=b+1;
while(s[b]!=','&&s[b]!='}')
{
b++;
}
mp[ans]=pii(a,b-1);
if(s[b]=='}')
break;
else
b++;
}
ans=u;
}
ans=(ans*p+idx(s[b]))%mod;
b++;
}
}
int main()
{
int t;
scanf("%d",&t);
int k;
while(t--)
{
mp.clear();
scanf("%s",s);
b=1;
dfs(0);
scanf("%d",&k);
for(int i=0;i<k;i++)
{
scanf("%s",str);
ull ans=0;
int len=strlen(str);
for(int i=0;i<len;i++)
{
ans=(ans*p+idx(str[i]))%mod;
}
if(mp.count(ans))
{
for(int j=mp[ans].first;j<=mp[ans].second;j++)
printf("%c",s[j]);
printf("\n");
}
else
printf("Error!\n");
}
}
return 0;
}