zoj 3462

时间:2023-03-08 19:45:23
 #include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <bitset>
#include <map>
#include <string>
#define maxn 1050
#define maxk 9000
using namespace std; int n,k;
long long int size[maxn];
bitset<maxn> mask; //用来记录最后出现搜索的tags的编号;
map <string,bitset<maxn> > mmap;
map <string,bitset<maxn> >::iterator iter; int main()
{
// if(freopen("input.txt","r",stdin)== NULL) {printf("Error\n"); exit(0);}
while(cin>>n){
mmap.clear();
char str[maxn];
for(int i=;i<=n;i++){
scanf("%s %lld",str,&size[i]);
char *p,*q;
p = str;
while((p = strchr(p,'[')) != NULL){
q = strchr(p,']');
string temp = string(p+,q);
mmap[temp].set(i);
p = q;
}
}
cin>>k;
for(int i=;i<=k;i++){
scanf("%s",str);
char *p,*q;
p = str;
mask.set();
while((p = strchr(p,'[')) != NULL){ //***
q = strchr(p,']');
string temp = string(p+,q);
iter = mmap.find(temp);
if(iter == mmap.end()){
mask.reset();
break; //*****
}
else mask &= iter->second;
p = q;
}
long long int ans = ;
for(int i=;i<=n;i++){
if(mask[i]) ans += size[i];
}
printf("%lld\n",ans);
}
}
return ;
}