here is only basic implementation of problems for beginners. If you have any problem with any solution or any basic concept of programming or you want more efficient solution you can mail me.
my suggestion is not to copy and paste codes from here try to understand the logic and think why you were not able to solve it.

Saturday 8 November 2014

Saruman of Many Colours

problem statement is here


#include<stdio.h>
#include<string.h>
int main(){
long int a,b,t,i,j,sum,n,u,k;
scanf("%ld",&t);
while(t--){
j=1;
sum=0;b=0;u=0;
char ar[25000];
scanf("%ld %ld",&n,&k);
scanf("%s",ar);
for(i=0;i<n;i++){
if(ar[i]>96 && ar[i]<123){
if(ar[i]==u){
b++;
if(b==k){
j=0;
b=0;
u=0;
}
}else{
sum++;
u=ar[i];
b=1;
if(b==k){
j=0;
b=0;
u=0;
}
}
}
}
if(j==1){
printf("-1\n");
}else{
printf("%ld\n",sum);
}
}
return 0;
}

3 comments:

  1. this is a wrong solution your code fails at following input:
    5 3
    rgggr
    your code output:-1
    correct output:3

    ReplyDelete
  2. your code is wrong
    test case for which it fails:
    5 3
    rgggr
    code output:-1
    correct output:3

    ReplyDelete
  3. your code is wrong
    test case for which it fails:
    5 3
    rgggr
    code output:-1
    correct output:3

    ReplyDelete