HASH保存每个KEY对应的值的起始位置和结束位置,最后直接输出就可以了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
long long mv;
map<long long,pair<long long,long long> > G;
string s;
long long op(char n)
{
if(n>='0'&&n<='9')return n-'0';
else if(n>='A'&&n<='Z')return n-'A'+11;
else if(n>='a'&&n<='z')return n-'a'+45;
else if(n=='.')return 62;
return 63;
}
void dfs(long long n)
{
long long temp=n;
while(s[mv]!='}')
{
mv++;
if(s[mv]=='}')return;
n = temp;
while(s[mv]!=':')n=n*123+op(s[mv++]);
long long left = ++mv;
if(s[mv]=='{')
dfs(n*123+62);
else
while(s[mv+1]!=','&&s[mv+1]!='}')mv++;
G[n] = make_pair(left,mv);
mv++;
}
}
int main()
{
long long T;
cin >> T;
while(T--)
{
cin >> s;
mv = 0;
G.clear();
dfs(0);
long long n;
cin >> n;
while(n--)
{
string str;
cin >> str;
long long nu = 0;
for(int i=0;i<str.length();i++)
{
nu=nu*123+op(str[i]);
}
if(G.count(nu))
{
for(int i = G[nu].first;i<=G[nu].second;i++)
cout<<s[i];
cout<<endl;
}
else
cout<<"Error!"<<endl;
}
}
return 0;
}