Page 11 of 11

Re: 11340 - Newspaper

Posted: Sat Apr 04, 2015 3:04 am
by anacharsis
Make life easier for yourself and remember to actually process newline characters.
They might pay for them after all... :)

Re: 11340 - Newspaper

Posted: Fri Aug 21, 2015 7:30 am
by RedGreenCode
When I was solving this problem in Java, I ran into problems with character encoding (as mentioned elsewhere in this thread) and I/O performance. I wrote up my findings in an editorial: http://www.redgreencode.com/solving-uva-11340-in-java/

Re: 11340 - Newspaper

Posted: Wed Oct 07, 2015 5:50 pm
by abir.khan
Hi there. my code is giving correct outputs against the critical inputs from udebug but whenever i submit the code it gets "runtime error". do i have a problem in my algorithm or is it the STL? pls pls help

Code: Select all

//UVA-11340 Newspaper 
#include<bits/stdc++.h>
using namespace std;

int main()
{
    int N;//number of tests
    int K;//number of paid characters
    long M;//number of lines of the article

    scanf("%d", &N);
    for(int i = 0; i < N; i++)
    {
        map<unsigned char, double> charToValue;
        map<unsigned char, double> :: iterator it;

        scanf("%d", &K);
        for(int j = 0; j < K; j++)
        {
            unsigned char ch;
            int ch_value;
            cin>>ch;
            cin>>ch_value;
            double temp = (ch_value * .01);
            charToValue[ch] = temp;
        }

        scanf("%ld\n", &M);//taking number of lines of article

        char str[M+1][10001];

        for(long j = 0; j < M; j++)
        {
            gets(str[j]);
        }

        long double sum = 0;
        for(long j = 0; j < M; j++)
        {
            //cout<<"j: "<<j<<" ";
            //cout<<sum<<"$"<<endl;

            int len = strlen(str[j]);

            for(int k = 0; k < len; k++)
            {
                //it = charToValue.find(str[j][k]);
                //if(it != charToValue.end())
                if(charToValue[str[j][k]] != '\0')
                {
                    sum += charToValue[str[j][k]];
                }
                else
                {
                    sum += 0;
                }
            }
        }

        cout<<sum<<"$"<<endl;
        charToValue.clear();
    }
    return 0;
}

Re: 11340 - Newspaper

Posted: Wed Dec 02, 2015 11:05 pm
by abid_iut
Seemed like a straight forward problem. Giving me correct answer too. Still getting WA. WHY???!!! Can anyone help? :(

Code: Select all

#include <iostream>
#include <cstdio>
#include <string>
#include <map>

using namespace std;

int calculateprice(string line, std::map<unsigned char, int> &P)
{
	std::map<unsigned char, int>::iterator it;
	int cost = 0.0;
	for ( int i=0; i<line.size(); i++ )
	{
		it = P.find(static_cast<char>(line[i]));
		if ( it != P.end())
		{
			cost = cost + it->second;
		} 
		else 
		{
			cost = cost + 0;
		}
	}
	return cost;
}

int main()
{	
	//freopen("input.txt", "r", stdin);
	int N, K, M;
	cin >> N;
	for( int i=0; i<N; i++ )
	{
		int total = 0;
		std::map<unsigned char, int> price;
		cin >> K;
		unsigned char c; int p;
		for ( int j = 0; j<K; j++)
		{
			cin >> c;
			cin >> p;
			price.insert(pair<unsigned char, int>(c, p));
		}
		cin >> M;
		string line;
		getline(cin, line);
		for ( int k = 0; k<M; k++ )
		{
			getline(cin, line);
			int res = calculateprice(line, price);
			total = total + res;
		}
		cout << (double)(total/100.0) << "$" << endl;
		price.clear();
	}
	return 0;
}

Re: 11340 - Newspaper

Posted: Sun Jan 24, 2016 8:41 pm
by trauM
I used unsigned char but still getting WA. What is the problem ?

Code: Select all

#include <bits/stdc++.h>

using namespace std;

int liste[1000];

int main(){
	int testcases,n,textlength,v,sum;
	unsigned char c;
	cin >> testcases;
	while(testcases--){
		for(int i=0;i<1000;i++)
			liste[i]=0;
		sum=0;
		cin >> n;
		for(int i=0;i<n;i++){
			cin >> c >> v;
			liste[c]=v; 
		}
		cin >> textlength;
		for(int i=-1;i<textlength;i++){
			scanf("%c",&c);
			while(c!=10){
				sum+=liste[c];
				scanf("%c",&c);
			}
		}
		if(sum%100>=10)
			cout << sum/100 << '.' << sum%100 << '$' << endl;
		else
			cout << sum/100 << ".0" << sum%100 << '$' << endl;
	}
	cout << endl;
	return 0;
}