UVa 11340 – Newspaper
News organizations pay for articles based on certain rules. Each character has its value (some characters may have a value of zero). The author's payment is the sum of the values of all the characters in the article. You need to determine the amount that the news organization must pay the author.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The first line contains an integer N (0 < N ≤ 5), representing the number of test cases. Each test describes an integer K (0 < K ≤ 100), representing the number of valuable characters. In the next K lines, there is a table with a character and its value (the value of the character is in cents). If a character is not found in the table, its value is zero. Next, there is an integer M (0 < M ≤ 150000). The next M lines contain the article itself. | For each test case, output the amount the news organization must pay for the article in the format "x.yy$", where x is the dollar amount without leading zeros and yy is the cents amount with a leading zero if necessary. |
1 7 a 3 W 10 A 100 , 10 k 7 . 3 I 13 7 ACM International Collegiate Programming Contest (abbreviated as ACM-ICPC or just ICPC) is an annual multi-tiered competition among the universities of the world. The ICPC challenges students to set ever higher standards of excellence for themselves through competition that rewards team work, problem analysis, and rapid software development. From Wikipedia. | 3.74$ |
Thought Process
Use a map to record the value of each character, and use a double to keep track of the total amount. Use getline to read each line of the string and evaluate each character; if the character is not recorded in the map, its value will be 0. Since the calculation is done in cents, the final answer needs to be divided by 100. If the answer is 0, output "0.00$".
Sample Code-ZeroJudge O231: Newspaper
#include <iostream>
#include <map>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
for (int i = 0; i<N; i++) {
int K, M;
double ans = 0;
cin >> K;
map<char, double>MAP;
for (int j = 0; j<K; j++) {
char ch;
double money;
cin >> ch >> money;
MAP[ch] = money;
}
cin >> M;
string s;
getline(cin, s);
for (int j = 0; j<M; j++) {
string str;
getline(cin, str);
for (int k = 0; k<str.length(); k++) {
ans += MAP[str[k]];
}
}
ans /= 100;
if (ans == 0) {
cout << "0.00$\n";
continue;
}
cout << ans << "$\n";
}
}
//ZeroJudge O231
//Dr. SeanXD