题目大意
吕弗·普自小从英国长大,受到骑士精神的影响,吕弗·普的梦想便是成为一位劫富济贫的骑士。
吕弗·普拿到了一份全国富豪的名单(不在名单上的都是穷人),上面写着所有富豪的名字以及他们的总资产,比如豪特斯·珀去年资产有86E,吕弗·普就会准备抢来资助贫困的伯恩兄弟……
现在吕弗·普做了$M$次打劫计划,每次要打劫若干个人,他想知道每次能打劫到的总资产是多少。
题目分析
简单的模拟,用$Map$会超时,改用trie树。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> #include<map> using namespace std;
inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; }
const int maxn=2000005; struct Tree { int child[26],bj,val; void clear() { memset(child,0,sizeof(child)); bj=0; } }; struct Trie { Tree tree[maxn]; int cnt; void insert(string s,int v) { int x=0; for(int i=0; i<s.length(); i++) { int y=s[i]-'a'; if(tree[x].child[y]==0)tree[x].child[y]=++cnt; x=tree[x].child[y]; } tree[x].bj=1; tree[x].val=v; } int find(string s) { int x=0; for(int i=0; i<s.length(); i++) { int y=s[i]-'a'; if(tree[x].child[y]==0)return -1; x=tree[x].child[y]; } if(tree[x].bj)return tree[x].val; else return -1; } } trie;
int n,m;
int main() { ios::sync_with_stdio(false); cin>>n; for(int i=1; i<=n; i++) { string s; int v; cin>>s>>v; trie.insert(s,v); } cin>>m; for(int i=1; i<=m; i++) { int x; bool bj=0; long long ans=0; cin>>x; for(int i=1; i<=x; i++) { string s; cin>>s; int x=trie.find(s); if(x==-1)bj=1; else ans+=x; } if(bj)puts("-1"); else printf("%lld\n",ans); } return 0; }
|