Page 1 of 11

11340 - Newspaper

Posted: Sun Nov 11, 2007 5:33 pm
by H_Hima
what the trick in this Problem.

this is my code in the contest?
I'm aware of space char and the final value too.
and got the res in long long.

I'm completely confused...

Code: Select all

woooooowh
what a great trick.
Got acc after that.

Thanks alot

Posted: Sun Nov 11, 2007 6:18 pm
by Robert Gerbicz
My c code is similar to your approach. I don't know the trick but it should be really crazy. On the contest: there were 384 submissions for this problem but only 45 AC.

Posted: Sun Nov 11, 2007 6:53 pm
by rio
Finally figured out the trick after 20 few submissions..

Character code has range 0 ~ 255, so use unsigned char.
(Is the spoiler ? :wink:)
----
Rio

Posted: Sun Nov 11, 2007 8:01 pm
by armansuleimenov
I used stl map, got TLE, now switched to simple hash table, got WA 3 times. what is wrong with my code?

Code: Select all

thanx all, got AC

Posted: Sun Nov 11, 2007 8:01 pm
by Robert Gerbicz
rio wrote: Character code has range 0 ~ 255, so use unsigned char.
(Is the spoiler ? :wink:)
----
Rio
Thanks, that was my problem! Now got AC.

Posted: Sun Nov 11, 2007 8:57 pm
by saman_saadi
armansuleimenov wrote:I used stl map, got TLE, now switched to simple hash table, got WA 3 times. what is wrong with my code?
According to rio's statement:
rio wrote: Finally figured out the trick after 20 few submissions..

Character code has range 0 ~ 255, so use unsigned char.
Then you can't use string because:
typedef basic_string<char> string;
so each element of string is char (not unsigned char) and you must handle this.

Posted: Mon Nov 12, 2007 5:39 am
by armansuleimenov
so each element of string is char (not unsigned char) and you must handle this.
What input routine should I use then?

I can't use the code below since the character can be whitespace (' '):

Code: Select all

ll cost;
unsigned char ch;
cin>>ch>>cost;
cnt[(int)ch]=cost;

Posted: Mon Nov 12, 2007 5:41 am
by armansuleimenov
should I use 64-bit integer to store the value of cents?

Easy Fix

Posted: Mon Nov 12, 2007 7:25 am
by baodog
Yes, you can read in char !! It just may be negative.

Simply add a constant !!! (128 or bigger)

Please, remove your code as good etiquette if you get ac.

Re: Easy Fix

Posted: Mon Nov 12, 2007 9:11 am
by saman_saadi
baodog wrote:Yes, you can read in char !! It just may be negative.

Simply add a constant !!! (128 or bigger)

Please, remove your code as good etiquette if you get ac.
In the contest time they release a clarification:
Solution for each test case must be printed on a separate line.
I.e.
Code:
10.10$
10.11$


Each character value is non-negative and at most 1000 cents worth.
According to the clarification character value is non-negative but I didn't see any statement about the range of characters in the problem. I got AC with unsigned char and don't handle negative characters. Are you sure there are negative characters in the input? If input has negative characters and the range of characters is -128 <= x <= 127 then I got AC in the contest time! After the contest I only change char to unsigned char (according to rio's statement) and got AC.

We know the range of char is:
-128 <= x <= 127
You say we must add a value 128 or higher to char so we have:
0 <= x + 128 <= 255
It's obvious 255 can't fit on char and it fit on unsigned char.[/code]

Posted: Mon Nov 12, 2007 9:21 am
by saman_saadi
armansuleimenov wrote: What input routine should I use then?

I can't use the code below since the character can be whitespace (' '):
You can use cin.get() method:

Code: Select all


unsigned char ch = cin.get();

But you must notice cin.get() reads white spaces ('\t', '\r', '\n', ' ') and don't ignore them so you must be careful And according to clarification you can't use int for storing characters values.

Re

Posted: Mon Nov 12, 2007 9:31 am
by Hojjat jafary
char ch = 255;
ch is negative one !
but the clarification said that the character codes are non-negative.
then you can use only char.

asd

Posted: Mon Nov 12, 2007 11:09 am
by darkos32
how to used cin.get() ?
i used like this for input :

2
av
sd

Code: Select all


scanf("%d\n",&m);
long total = 0;
while(m--){
unsigned char ch;
ch = cin.get();
cout << ch;
int cht = (int) ch;
}

but it prints nothing..and how to check the newline ?

thanks.

Re: asd

Posted: Mon Nov 12, 2007 11:23 am
by saman_saadi
darkos32 wrote:how to used cin.get() ?
i used like this for input :

2
av
sd

Code: Select all


scanf("%d\n",&m);
long total = 0;
while(m--){
unsigned char ch;
ch = cin.get();
cout << ch;
int cht = (int) ch;
}

but it prints nothing..and how to check the newline ?

thanks.
I think you must use cin instead of scanf because cin buffer and scanf buffer are different.
input:

2
av
sd

Code: Select all

	int n, ch1, ch2;

	for (cin >> n; n--;)
	{
		cin.get();	//feeding new line character in cin >> n
		ch1 = cin.get();
		ch2 = cin.get();
		cout << (unsigned char)ch1 << (unsigned char)ch2 << endl;
	}
for checking new line character using below code:

Code: Select all

ch = cin.get();
if (ch == '\n')
{
.
.
.
}
but I think it's better to write:

Code: Select all

if (ch == '\n' || ch == '\r')
I change unsigned char to int (because cin.get() return int) and submit it again and I found that no negative character exist in input data.[/code]

Posted: Mon Nov 12, 2007 11:36 am
by Hojjat jafary
another way is use gets( str_buff ) and use sscanf()
and cin.get( char );