Hey AK795 I have Ran Your Inputs with My Code . It generates same output as I have run an AC code of the contest . I have edited My code . I submitted it during the contest but It got WA all the time .
I just ran your code for certain test data, its giving me wrong outputs, i think probably you are missing the cases when special symbols are attached to the strings .
Your solution doesn’t produce correct results for many test cases. Eg-
1
hhhhaaaa aaa
Your output is ‘h’ while it should be ‘a’.
Also, your algorithm is highly inefficient, you are traversing the string for each possible character in [a-z], which isn’t required. Just keep a count of the characters present in the string and find the character with maximum number of occurrences.
The problem with your solution is that it doesn’t read input properly. Try printing the input and you’ll see the error, See here.
The problem is with gets();
int a[26];
memset(a,0,sizeof(a));
if s[i]>='A' and s[i]<='Z':
a[s[i]-65] += 1
elif s[i]>='a' and s[i]<='z':
a[s[i]-97] += 1
now traverse the array and check if two alphabets have same count then compare their ASCII values and print the output as required in the problem statement.
ans = a[25]
pos = 25
for i in range(24,-1,-1):
if a[i]>ans:
ans = a[i]
pos = i
print 'a' + (char)pos
One of the reasons, why your code is wrong, is because, your variable “t” should have been an integer, and not a char.
Since, you have declared it as a char, the variable is only allocated only 1 byte. But, when you are reading an integer value into it, 4-bytes of data are written. Now, that extra 3-bytes of data is overwriting data in some other memory location. (My test runs show that they are overwriting first three locations of your array ‘a’. Making your array a as {'\0', '\0', '\0', 'd', 'e', ...}). So, you basically end up never counting the a’s, the b’s and the c’s in your strings. (You can verify this by printing the contents of array a, before and after reading the value of t, and check whether they are same or not.)
For example, your code gives answer as d for the input string aaaad (during my test run)
You will need to wait until those problems are moved to the practice section (look under the section ‘Peer’) It might take some days (or even weeks) for problems from external contests hosted on codechef, to be available in the practice section.