## 11340 - Newspaper

Moderator: Board moderators

anacharsis
Learning poster
Posts: 69
Joined: Mon Feb 09, 2015 1:56 am

### Re: 11340 - Newspaper

Make life easier for yourself and remember to actually process newline characters.
They might pay for them after all...

RedGreenCode
New poster
Posts: 7
Joined: Wed May 13, 2015 3:41 am
Location: Seattle, WA, USA
Contact:

### Re: 11340 - Newspaper

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/
Deliberate practice techniques for software developers -- http://www.redgreencode.com/

abir.khan
New poster
Posts: 1
Joined: Wed Oct 07, 2015 5:44 pm

### Re: 11340 - Newspaper

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;
}
``````

abid_iut
Learning poster
Posts: 82
Joined: Wed Jul 16, 2008 7:34 am

### Re: 11340 - Newspaper

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;
}
``````
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/

trauM
New poster
Posts: 1
Joined: Sun Jan 24, 2016 8:38 pm

### Re: 11340 - Newspaper

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;
}``````