WA in TWSTR

someone please help me.
i am getting WA at 0.75 sec…
http://www.codechef.com/problems/TWSTR
my attempt - http://ideone.com/VLI5Jx#view_edit_box

Try this input
2 ( number of recipes )
ab 2 (recipe 1 with priority )
aa 1 ( recipe 2 with priority )
1 ( number of queries )
a ( the query )
Your answer is “aa” while correct answer is “ab” .
You are sorting priorities but that is not affecting the actual recipes , they remain in the same order . You have to make a single object Recipe with name and priority and sort them according to priority . Then the last recipe which matches can be outputted .

1 Like

@vineetpaliwal can you suggest a method so that the string is mapped to its priority after sorting priority in c language?

@the_c0der : you should declare a “struct” having recipe name and priority as fields . You should make array of such “struct” values . You should define a comparison function for two values of struct type . Then you make the sort call passing it the arrays of “struct” and the comparison function etc .

1 Like

@the_c0der : if you make a “struct” as I told you earlier and make an array of that data type and define the comparison function on that “struct” as comparing the priorities it will do the job .
Suppose
struct {
int priority ;
String recipe ;
}
eg. Suppose this your array after reading input .
{ “vineet” , 1} , { “coder” , 3 } , {“recipe” , 2 } .
After sorting it will look like :
{ “vineet” , 1 } , { “recipe” , 2 } , { “coder” , 3 } .
So when you encapsulate two things into one data type by using “struct” , they always move together , so they are automatically mapped . It is only when you define two different arrays , sorting one array will no affect the other .
Regarding the sorting function to be used use the normal sorting function which takes array , comparison function as parameters .