#include<stdio.h>
#include<string.h>
int main()
{
int t;
scanf("%d",&t);
while(t–)
{
char s[201];
scanf("%s",s);
char m[]={‘a’,‘b’,‘c’,‘d’,‘e’,‘f’,‘g’,‘h’,‘i’,‘j’,‘k’,‘l’,‘m’,‘n’,‘o’,‘p’,‘q’,‘r’,‘s’,‘t’,‘u’,‘v’,‘w’,‘x’,‘y’,‘z’,‘A’,‘B’,‘C’,‘D’,‘E’,‘F’,‘G’,‘H’,‘I’,‘J’,‘K’,‘L’,‘M’,‘N’,‘O’,‘P’,‘Q’,‘R’,‘S’,‘T’,‘U’,‘V’,‘W’,‘X’,‘Y’,‘Z’};
int i=0,cost=0;
while(i<strlen(m))
{
int j=0,count=0;
while(j<strlen(s))
{
if(m[i]==s[j])
count=count+1;
j++;
}
cost=cost+count/2;
if(count%2!=0)
cost=cost+count%2;
i++;
}
printf("%d\n",cost);
}
return 0;
}
Please paste code here properly using the code tool or give the ideone link. That is better to read and understand.
didn’t your code get accepted on codechef(in the first try)? what is the doubt you are having?
As you are using strlen(m) you may need to put ‘\0’ as the last element in array m[]. I don’t think it will consider it by default. strlen() will always calculate the length till it finds ‘\0’.
If you assign m = “abc…XYZ”; then you don’t have to mention ‘\0’ explicitly. So that may be the reason for your code running wrong.