Why is it giving WA for NAME1 , when using string class, and AC with char array

the problem statement here… http://www.codechef.com/MAY13/problems/NAME1

and my code using string class … http://www.codechef.com/viewsolution/2134447 (WA)

my code using char array … http://www.codechef.com/viewsolution/2122637 (AC)

I replaced your dynamic arrays with static ones here - http://www.codechef.com/viewsolution/2173248 you can do some experiments to find out what exactly caused the problem :wink:

1 Like

is the link broken ?

1 Like

thanks for letting me know, fixed :wink:

Can we define an array using int noc[t] , where ‘t’ is a variable.
If I am not wrong, this is definitely not allowed in C . I am more familiar with C , I have to admit. In C , you have to give the specific size , for eg: int noc[10] , otherwise, you have to dynamically allocate by using malloc.

How is C++ different from C, in this aspect. Pls answer …

C and C++ are very similar and in both languages this construct is available :wink: as my code shows…

Main difference in int a[n] and int* a = new int[n] is “where the memory is taken from”. In first case it is stack, in second it’s heap.

1 Like

@betlista >> Actually @sumanth232 is right, standard C doesn’t support int noc[t] where t is dynamically taken as input, but post C99 it does support this. Also, you’re right about the stack and heap allocations. +1

1 Like

@bugkiller , thats what I am looking for… you confirmed it… thank you, +1.
So, codechef accepts VLAs ( variable length arrays - int a[n] )

I found out the problem in my code…
After this line … int *hash = new int[26];
I added this …
for(int i=0; i<26; i++)
{
hash[i] = 0;
}
AND IT GAVE AC…

So, can somebody throw light on this… Is this really necessary.
I thought , all the elements of hash array will be set to 0 , by default.

Infact, I tested it on my ubuntu 12.04 , without the above for loop,
and they were actually set to 0 by default

@sumanth232 >> Not only codechef, but new compilers also support that.
If you declare a variable/array globally, then it is initialized to zero by default. Declaring locally might initialize the variable with some garbage value. Thats what I know. I might be incorrect. You might want to refer to this http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c

1 Like

@bugkiller , i have asked a question from you in AMSGAME2 - Editorial link. pls ans.