UVA – 11340 – Newspaper

Problem Statement : http://uva.onlinejudge.org/external/113/11340.html

Type : Adhoc

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int main() {
	int T, N, c[258], p;
	char ch;
	scanf("%d\n", &T);
	while(T-- && scanf("%d\n", &N)==1) {
		memset(c, 0, sizeof c);
		while (N-- && scanf("%c %d\n", &ch, &p)==2) c[ch + 128] = p;
		scanf("%d\n", &N);
		while (N--) while ((ch = getchar()) != '\n')
				c[257] += c[ch + 128];
		printf("%d.%02d$\n", c[257] / 100, c[257] % 100);
	}
	return 0;
}
Advertisements

5 thoughts on “UVA – 11340 – Newspaper

  1. Jonathan says:

    Beautiful solution.

  2. Daniel says:

    Question. Why is it necessary to add 128 when you use the array c? My solution is almost like this but without the + 128 and I keep getting WA. Thank you.

  3. Tú Nguyễn says:

    because the range of char is from -128 ~ 127.

  4. Siddartha Mallick says:

    thanks for the info (because the range of char is from -128 ~ 127.).. Didn’t know it

  5. Thanks , finally i got accepted……………

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: