Page 10 of 11

Re: 11340 - Newspaper

Posted: Tue Nov 18, 2014 11:20 pm
by brianfry713
Print a newline char at the end of a line.
Try:
printf("%.2f$\n",sum/100);

UVa 11340 - Newspaper

Posted: Fri Nov 21, 2014 9:39 pm
by VictorProgrammerS
I have a problem with this excersice, mi code is:
http://uva.onlinejudge.org/index.php?op ... oblem=2315

Code: Select all

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.StringTokenizer;

class Main {

	BufferedReader in;
	PrintWriter out;
	HashMap<Character, Integer> payments;
	double total;

	public static void main(String[] args) throws IOException {
		new Main().run();
	}

	void run() throws IOException {
		in = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out, true);
		solve();
		out.flush();
	}

	void solve() throws IOException {
		payments = new HashMap<Character, Integer>();
		int N = Integer.parseInt(in.readLine());
		for (int i = 0; i < N; i++) {
			int K = Integer.parseInt(in.readLine());
			for (int j = 0; j < K; j++) {
				String line = in.readLine();
				StringTokenizer str = new StringTokenizer(line, " ");
				while (str.hasMoreTokens()) {
					char[] key = str.nextToken().toCharArray();
					payments.put(key[0], Integer.parseInt(str.nextToken()));
				}
			}
			int M = Integer.parseInt(in.readLine());
			for (int l = 0; l < M; l++) {
				payment(in.readLine());
			}
			total /= 100;
			out.printf("%.2f$\n", total);
		}
	}

	double payment(String statement) {
		for (int j = 0; j < statement.length(); j++)
			if (payments.containsKey(statement.charAt(j)))
				total += payments.get(statement.charAt(j));
		return total;
	}
}
The UVa Jugde return WA and I don't know why
Help me! please

Re: UVa 11340 - Newspaper

Posted: Sat Nov 22, 2014 5:18 pm
by lighted
Post in existing thread. Use search by problem number (11340). Check your input here. http://www.udebug.com/UVa/11340

Re: 11340 - Newspaper getting so much WA WA plz help me

Posted: Sat Nov 22, 2014 7:10 pm
by cse dipto

Code: Select all

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <sstream>
#include <utility>

using namespace std;

long long int ch_counter[128] ;

int main()
{
    long long int i,j,k,t_case,t_case2,ch_value,str_line,sum ;
    //double result,sum ;
    char  str[10005] ,ch,str1[100] ;
    string s;
    cin >> t_case ;
    for(i = 1 ; i <= t_case ; i++)
    {
        memset(ch_counter , 0 , sizeof(ch_counter)) ;
        cin >> t_case2 ;
        getchar() ;
        for(j = 1 ; j <= t_case2 ; j++)
        {
            int flag = 0 ;
            gets(str1) ;
            int len = strlen(str1) ;
            int index = 0 ;
            for(k = 0 ; k < len ; k++)
            {
                if(str1[0] == ' ')
                {
                    flag = 1 ;
                    break ;
                }
                if(str1[0] != ' ')
                {
                    ch = str1[0] ;
                }
                if(str1[k] >= '0' && str1[k] <= '9')
                {
                    s[index++] = str1[k] ;
                }
            }
            if(flag == 0)
            {
                s[index++] = '\0' ;
                ch_value = atoi(s.c_str()) ;
                ch_counter[ch] = ch_value ;
            }
        }
        cin >> str_line ;
        getchar() ;
        sum = 0 ;
        for(j = 1 ; j <= str_line ; j++)
        {
            gets(str) ;
            int len = strlen(str) ;
            for(k = 0 ; k < len ; k++)
            {
                if(ch_counter[str[k]] > 0)
                {
                    sum = sum + ch_counter[str[k]] ;
                }
            }
        }
        printf("%.2lf$\n",sum/100.0) ;
    }
    return 0;
}

Re: 11340 - Newspaper pls help getting so much WA

Posted: Sat Nov 22, 2014 7:30 pm
by cse dipto

Code: Select all

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <sstream>
#include <utility>

using namespace std;

long long int ch_counter[128] ;

int main()
{
    long long int i,j,k,t_case,t_case2,ch_value,str_line,sum ;
    //double result,sum ;
    char  str[10005] ,ch,str1[100] ;
    string s;
    cin >> t_case ;
    for(i = 1 ; i <= t_case ; i++)
    {
        memset(ch_counter , 0 , sizeof(ch_counter)) ;
        cin >> t_case2 ;
        getchar() ;
        for(j = 1 ; j <= t_case2 ; j++)
        {
            int flag = 0 ;
            gets(str1) ;
            int len = strlen(str1) ;
            int index = 0 ;
            for(k = 0 ; k < len ; k++)
            {
                if(str1[0] == ' ')
                {
                    flag = 1 ;
                    break ;
                }
                if(str1[0] != ' ')
                {
                    ch = str1[0] ;
                }
                if(str1[k] >= '0' && str1[k] <= '9')
                {
                    s[index++] = str1[k] ;
                }
            }
            if(flag == 0)
            {
                s[index++] = '\0' ;
                ch_value = atoi(s.c_str()) ;
                ch_counter[ch] = ch_value ;
            }
        }
        cin >> str_line ;
        getchar() ;
        sum = 0 ;
        for(j = 1 ; j <= str_line ; j++)
        {
            gets(str) ;
            int len = strlen(str) ;
            for(k = 0 ; k < len ; k++)
            {
                if(ch_counter[str[k]] > 0)
                {
                    sum = sum + ch_counter[str[k]] ;
                }
            }
        }
        printf("%.2lf$\n",sum/100.0) ;
    }
    return 0;
}

Re: 11340 - Newspaper

Posted: Sun Nov 23, 2014 5:45 pm
by lighted
Don't double post.

Something strange. :-? When i made your array str global judge gave accepted. :)
rio wrote:Finally figured out the trick after 20 few submissions..

Character code has range 0 ~ 255, so use unsigned char.
(Is the spoiler ? :wink:)
faiem wrote::D
In this problem must use "unsigned char"...Or u will get WA...again and again just like me.
:oops:
brianfry713 wrote:Don't assume that the characters in this problem have an ASCII value less than 128. Use an unsigned char.
brianfry713 wrote:Read this thread:http://acm.uva.es/board/viewtopic.php?t=24824

A signed 1 byte char always has range of -128 to 127.

For some reason the problem setter decided to use more than the standard range of 0 to 127 characters in the judge's input.
I read so many posts in this thread saying to use unsigned char. But I got accepted using char and i didn't add 128 to make it positive. Here is my code. http://ideone.com/YMbgGq
Is judge's input changed? Brianfry can you fix this? (In my accepted code if i make char array local it gives RuntimeError. Very strange. :-?)

Re: 11340 - Newspaper

Posted: Wed Nov 26, 2014 2:57 am
by brianfry713
If you are reading and writing outside of an array boundary you are going to get undefined results. Just because you manage to get AC doesn't mean it's correct.
The chars in the judge's input could be negative if you read them as a signed char.
You should either read them as an unsigned char or convert to an int and add 128. Make your array size 256.

Re: 11340 - Newspaper

Posted: Wed Nov 26, 2014 1:47 pm
by lighted
In my accepted code i read characters as signed char, so as you mentioned it can be negative or greater than 127.
I added 2 lines to check values of characters and if negative of greater 127 made infinite loop. Again ACC, not TLE. Here's code. http://ideone.com/rwziCe
Can you check it?

Re: 11340 - Newspaper

Posted: Sun Nov 30, 2014 1:12 pm
by shubhamgarg1
Getting wrong answer . Please help me i used +127 so as to be sure

Code: Select all

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
long long n,i,j,a[257],p,t,k,z,l;
double s;
 char b[100000];
char ch;
scanf("%lld",&t);
while(t>0)
{
t--;
for(i=0;i<=256;i++)
a[i]=0;
scanf("%lld",&n);
for(i=0;i<n;i++)
{
    while(getchar()!='\n');
scanf("%c",&ch);
scanf("%lld",&p);

a[ch+128]=p;
}
scanf("%lld",&k);

s=0;
//printf("hui  %d",k);
for(j=0;j<k;j++)
{
	//printf("hui");
    scanf("%*c%[^\n]",b);
    z=strlen(b);
    //printf("%d",z);
    for(l=0;l<z;l++)
    {
        s=s+(double)a[b[l]+128];
        
    }
}
s=s/100;
printf("%.2lf$\n",s);

}
return 0;
}

Re: 11340 - Newspaper

Posted: Sun Nov 30, 2014 2:00 pm
by lighted
Change reading to

Code: Select all

scanf("%lld",&k);
while(getchar()!='\n');

for (s = j = 0; j < k; j++)
{
    cin.getline(b, 100000);

    for (l = 0; b[l]; l++) s += a[ b[l] + 128 ];
}

Re: 11340 - Newspaper

Posted: Sun Nov 30, 2014 7:18 pm
by unreleased
whats wrong in this code.........??

Code: Select all

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <iterator>
#include <map>
#include <set>

#define mx 1000000

#define D   double
#define L   long
#define LL  long long
#define ULL unsigned long long

#define isc1(a)      scanf("%d", &a)
#define isc2(a,b)    scanf("%d%d", &a, &b);
#define isc3(a,b,c)  scanf("%d%d%d", &a, &b, &c)
#define llsc1(a)     scanf("%I64d", &a)
#define llsc2(a,b)   scanf("%I64d%I64d", &a, &b)
#define llsc3(a,b,c) scanf("%I64d %I64d %I64d", &a,&b,&c)

#define f(a,n)  for(a=0; a<n; a++)
#define all(a)  a.begin(), a.end()
#define ms(arr) memset(arr, 0, sizeof(arr))
#define cl(a)   a.clear()
#define sz(a)   a.size()

#define sc scanf
#define pf printf
#define pu push_back
#define pb pop_back
#define vc vector

using namespace std;

int main()
{
    L a,b,d, x;
    L c;
    L tst, pnum, line, cval, len;
    L val[300];
    string str;
    char ch;
    D tot;
    sc("%ld",&tst);
    while(tst--)
    {

        ms(val);
        sc("%ld",&pnum);
        f(a, pnum)
        {
            cin>>ch>>cval;
            d=ch;
            val[d]=cval;
        }


        sc("%ld\n", &line); c=0;
        for(a=0; a<line; a++)
        {
            getline(cin,str);
            len=sz(str);

           for(b=0; b<len; b++)
            {
               //if(val[str[b]])
                c+=val[str[b]];

            }
        }
        tot=(D)c/100.00;
        pf("%0.2lf$\n", tot);
    }


    return 0;
}

Re: 11340 - Newspaper

Posted: Mon Dec 01, 2014 7:30 am
by shubhamgarg1
Thanks lighted .
My code got accepted. But still not able to figure out what was wrong with scanf("%*c%[^\n]",b);
As i have used this on other question on spoj and have got accepted.

Re: 11340 - Newspaper

Posted: Wed Dec 03, 2014 5:08 am
by brianfry713
lighted I don't have access to the judge's input. The table contains only ASCII values between 0 and 127, but the article does not. A signed char can not be greater than 127.

unreleased, read this thread. If you are reading the input as a signed char you will get negative values in the article.

Re: 11340 - Newspaper

Posted: Wed Dec 03, 2014 6:18 pm
by lighted
Thanks Brianfry for your reply. Everything become obvious. :)

In the code above i checked if table contains negative value when reading characters as signed char. So table doesn't have characters with negative value. Yes, you right that signed char cannot be greater than 127. So it is unnecessary to check it.

I also checked article. And as you said it contains negative values. This was the reason why i got RE when i made char array local. (When made global it accidentaly got accepted).

But i still don't see a reason to use unsigned char. Table contains only standart characters between 0 and 127, only for these characters price would be paid. We should not consider other characters, especially those non-standart characters that greater than 127. I just added checking if character in article is between 0 and 127. :)

Re: 11340 - Newspaper

Posted: Wed Dec 03, 2014 9:16 pm
by brianfry713
There are many ways to parse input that work. Signed or unsigned char is fine as long as you handle the values that don't fall between 0 and 127 correctly.