-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path10295.cpp
executable file
·64 lines (52 loc) · 1.06 KB
/
10295.cpp
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
/* Problem: Hay Points UVa 10295
Programmer: Md. Mahmud Ahsan
Description: Sorting + Searching
Compiled: Visual C++ 7.0
Date: 18-09-05
*/
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct word{
string str;
int dollar;
}dics[1200];
inline bool comp(word a, word b){
if (a.str > b.str) return false;
return true;
}
int binarySearch(string a, int h){
int low, mid, high = h;
low = 0;
while (low <= high){
mid = (high + low) / 2;
if (dics[mid].str == a)
return mid;
else if (dics[mid].str < a)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main(){
int m, n, k, i, dollar;
string a;
while (cin >> m >> n){
for (i = 0; i < m; ++i)
cin >> dics[i].str >> dics[i].dollar;
sort(dics, dics+m, comp);
for (i = 0; i < n; ++i){
dollar = 0;
while (cin >> a){
if (a == ".") break;
k = binarySearch(a, m);
if (k != -1)
dollar += dics[k].dollar;
}
cout << dollar << endl;
}
}
return 0;
}