UVA11340 Newspaper【输入流+map】

News agency pays money for articles according to some rules. Each character has its own value (somecharacters may have value equals to zero). Author gets his payment as a sum of all character’s valuesin the article. You have to determine the amount of money that news agency must pay to an author.

Input

The first line contains integer N (0 < N ≤ 5), it is a number of tests. Each test describes an integerK (0 < K ≤ 100), the number of paid characters. On next K lines there are table of paid charactersand its values (character values are written in cents). If character can not be found in the table, thenits value is equal to zero. Next, there is integer M (0 < M ≤ 150000). Next M lines contain an articleitself. Each line can be up to 10000 characters length. Be aware of a large input size, the whole inputfile is about 7MB.

Output

For each test print how much money publisher must pay for an article in format ‘x.yy$’. Where x isa number of dollars without leading zeros, and yy number of cents with one leading zero if necessary.Examples: ‘3.32$’, ‘13.07$’, ‘71.30$’, ‘0.09$’.

Sample Input

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.

Sample Output

3.74$


问题链接UVA11340 Newspaper

问题简述参见上文。

问题分析这是一个给出字符的权值,计算文章重量的问题,关键在于如何计算

程序说明

假定报纸中的字符都是可见字符,那么每个字符ASCII值会小于128,因此数组mp[]有128个元素是够的。

mp也可以用STL的容器类map来实现,但是比起数组来效率要低很多,还是数组简洁快速。数组mp[]功能上相当与map。

题记:(略)

参考链接:(略)


AC的C++语言程序如下:

/* UVA11340 Newspaper */

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

const int N = 128;
const int N2 = 10000;

int mp[N+1];     // map
char s[N2+2];

int main()
{
    int n, k, m, val;
    char c;

    scanf("%d", &n);
    while(n--) {
        memset(mp, 0, sizeof(mp));

        scanf("%d\n", &k);
        for(int i=1; i<=k; i++) {
            scanf("%c%d\n", &c, &val);
            mp[(int)c] = val;
        }

        scanf("%d\n", &m);
        long long sum = 0;
        while(m--) {
            while ((c = getchar()) != '\n')
                sum += mp[(int)c];
        }

        printf("%.2f$\n", (double)sum / 100);
    }

    return 0;
}




posted on 2017-07-27 10:23  海岛Blog  阅读(150)  评论(0编辑  收藏  举报

导航