Round A 2020 - Kick Start 2020

PL HELP ME OUT IN FINDING A MISTAKE …

using namespace std;
int check (string s1,string s2)
{int temp=0;
for(int i=0;i<s1.size();i++)
{
if(s1[i]==s2[i])
{
temp++;
}else{
break;
}
}
return temp;
}

int main()
{
int t;
cin>>t;
for(int l=0;l<t;l++)
{
int n,k;
cin>>n>>k;
string s[n];
for(int i=0;i<n;i++)
{
cin>>s[i];
// cout<<s[i]<<" ";
}
sort(&s[0],&s[n]);

``````  int score=0,i=0;
while((n-i-1)>0)
{
int count=0;
for(int j=i;j<i+k-1;j++)
{
int temp =check(s[j],s[j+1]);

if(j==i)
{
count=temp;
continue;
}
else
if(count>temp)
{
count=temp;
}
}
score=score+count;
i=i+k;
}
cout<<"Case #"<<l+1<<": "<<score<<endl;
``````

}
}

What is the logic behind your code?
I used a trie and I made a video solution for it - https://youtu.be/2Kb_QyPEMd0

I HAVE FIRST SORTED THE STRINGS ARRAY AND THEN DIVIDED INTO GROUPS USING WHILE LOOP
THEN I HAVE CALCULATED THE MIN NUMBER OF COMMON ELEMENTS IN THE GROUPS
AT LAST ADDING UP ALL THE GROUPS SCORE…
I WILL GO THROUGH YOUR SOLUTION VIDEO
BUT STILL CHECK MY CODE IT IS GIVING THE RIGHT OUTPUT FOR THE GIVEN TEST CASES
PL. COMPILE AND CHECK THE OUTPUT
IT IS GIVING WRONG ANSWER

Consider this test case
N=4, K=2
aabb
aabccccccc
aabccccccc
abc

This input is sorted and according to your code string 1 and 2 will be grouped and 3 and 4 will be group and total score will be 3+1=4.
However if we group 1 and 4 and group 2 and 3, the total score will be 1 + 10 = 11

1 Like