DEVSTR - Editorial

Hi,

Can the editorialist, or someone else, explain in some detail the general idea of a DP solution?

During the contest I tried to think of one but wasn’t very successful :frowning:

Best,
Bruno

Hello,

I tried to implement the above algorithm and wrote this code.

http://www.codechef.com/viewsolution/6978550

It works great for whatever input I give but codechef shows WA in all test cases.

It would be great if someone could tell me the mistake.

Regards

Priyank

@priyankpalod

Wrong approach.
Check for n=20 k=4
11111111111111110000.

You will automatically end up with knowing what you did wrong.

@shivam9753

Thank you for your help. Got the solution accepted :slight_smile:

please
give a test case for k=1 why same logic cannot be applied for k==1 i could not figure out this

please give testcase for k=1

please give testcase for k=1

I’m getting a NZEC for my solution to this problem using the same logic, even though it’s working on my computer! :confused:
I also tried using the maximum value of n to check if the memory heap gets depleted but that isn’t the case!
can anyone help?
here’s the link to the solution : CodeChef: Practical coding for everyone

curious1ab3: 1010101010110101010101

Can someone point out where m i going wrong?? CodeChef: Practical coding for everyone
Its getting partially accepted… I couldn’t think of a test case where it will go wrong

hello i’ve posted a query before(please refer above),i’d used the same logic stated in the editorial,but my solution got only partially accepted,so it must have been some implementation problem,but since past two days i’ve been coding another solution using some other implementation method but its not being successful.
Could anyone please point out the error in my code,you may try running my code against as many test cases because i would appreciate it even more if i could find which test case my code went wrong Links-1.CodeChef: Practical coding for everyone
2.ShqsUA - Online C Compiler & Debugging Tool - Ideone.com(in case link 1 doesn’t open)
thanks in advance.
P.S.-my solution got partially accepted

my code is exceeding time limit…Dont know why??

#include<stdio.h>
#include<string.h>
int main()

{
int cs, n, k, i = 0, j = 0, zero = 0, one = 0,t = 0;

    char str[100][20], temp [6], opr[100] = {0};

    scanf("%d",&cs);
    while(cs)
    {
    __fpurge(stdin);
    gets(str[t]);

    while(str[t][i] != ' ')
    {
            temp[i] = str[t][i];
            i++;
    }
    temp[i] = '\0';
    n = atoi(temp);
    i++;
    while(str[t][i] != '\0')
    {
            temp[j] = str[t][i];
            j++;
            i++;
    }
    temp[j] = '\0';
     k = atoi(temp);
   __fpurge(stdin);
    gets(str[t]);
    i=0;
    while(n)
    {
            if(str[t][i] == '0')
            {
                    one = 0;
                    zero++;
                    if(zero == k+1)
                    {       zero = 0;
                            str[t][i] = '1';
                            i--;
                            n++;
                            opr[t]++;
                    }
            }
            else
             {       zero = 0;
                    one++;
                    if(one == k+1)
                    {       one = 0;
                            str[t][i] = '0';
                            i--;
                            n++;
                            opr[t]++;
                    }
            }
    i++;
     n--;


    }

cs–;
t++;
i=0;
j=0;
}
while(t–)
{

    printf("%d\n",opr[i]);
    puts(str[i]);
    i++;
    }

}
.
.
.
.pls help if anyone???

@sukkhi
There are needless operations you used in you code.
There is a better approach.

@shivam9753 :

I applied same algorithm, my soln got 20 marks as well, but I am not able to figure out where I am going wrong

Could you please help me.
my soln: http://www.codechef.com/viewplaintext/6939194

@sunya

I think, here is the problem.

for (int x = 0; x < tmpCount; x++)
{
sequence[i + 1 + x * K] = opositChar;

suppose if k=3,n=13.
0001111111111.

Your o/p would be flips=2
string 0001011101111.

So the string after the flips…will be contradicting the condition(more than k consecutive characters not allowed).

                                                                                       REGARDS.

@shivam9753
would be much grateful if you could address my previous query also ^

hello i’ve posted a query before(please refer above),i’d used the same logic stated in the editorial,but my solution got only partially accepted,so it must have been some implementation problem,but since past two days i’ve been coding another solution using some other implementation method but its not being successful. Could anyone please point out the error in my code,you may try running my code against as many test cases because i would appreciate it even more if i could find which test case my code went wrong Links-1.CodeChef: Practical coding for everyone 2.ShqsUA - Online C Compiler & Debugging Tool - Ideone.com(in case link 1 doesn’t open) thanks in advance. P.S.-my solution got partially accepted

thanks in advance :slight_smile:

@oldschool

I think its faulty implementation.
You cant flip anywhere.
Do it as follow.

Count the consecutive 1’s or 0’s if they are greater than K, name it K1.(K1>K)

If K1 mod K not equals to 0

flip every K+1th character.

Else if K1 mod K==0

Start with flipping kth character first

then flip every (k+1)th character.

hmm thanks very much @shivam9753,i had understood the editorial in quite a different way and thanks for rectifying that error
i still have one thing in my mind if you would be so kind to clear them too…
actually i’m not flipping anywhere,i flip when in a continuous substring count >=k
Case 1: count>k at a character “inside” the substring(i.e a character not the last character of the substring),in this case i flip the current character,count is reset to 0
Case 2:count>k at the last character of substring,in this case the previous character is flipped(we should avoid flipping the last character of substring right?),count again reset to 0
eg.-in 00000011,k=2,the 0s at index 2 and 4 will be flipped(edited substring-00101011)
so in which way/case/path can this method go wrong?
thanks very much again

1 Like

@old_school
Great to see that you are still trying to do the hair-skin test of the problem even after getting 100 points :).
Your case1 and case2 are quite not clear to me,although this approach seems to be correct.

yeah thanks @shivam9753 for helping all along,those 100 points i had got were implementation of my friend’s logic who had got AC with 100 points,but i was still unsatisfied with my code not being AC and so tried that again,anyways looks like i’d have to just give up on the code and move on
thanks once again :slight_smile: