12599 - Black and White

All about problems in Volume 125. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
Faria049
New poster
Posts: 2
Joined: Thu Dec 12, 2013 6:54 pm

12599 - Black and White

Post by Faria049 »

I can't figure out why I am getting presentation error in this. Need help

Code: Select all

 
#include <stdio.h>
#include <string.h>

int main(){
    int t,length,i,j,k,b,w,black,white,N,x,flag,p;
    int index[10000];
    scanf("%d", &t);
    while (t!=0){

        flag=0;
        x=0;
        char a [10000];
        int b[10000], w[10000];
        j=0; k=0;
        scanf("%s",a);
        length = strlen(a);
        for (i=0;i<length;i++){

            if (a[i]=='B') {
                b[j++]=i+1;
                x=i;
            }
            else {
                if (i==0){printf("IMPOSSIBLE\n"); flag=1; break; }
                else {
                    w[k++]=i+1;
                }
            }
        }

        if (x==(i-1)) printf("IMPOSSIBLE\n");

        else if (flag==0){

            black=j-1;
            white=k-1;

            N=0;
            j=0; i=0; k=0;
            index[i]=b[j];

            while((b[j+1]-b[j])==1)
            {
                index[++i]=b[++j];

            }

            i++;


            for (k=0; k<white; k++){
                index[i++]=w[k];

            }

            if (black==j){

                N=1;
                while ((k-1)!=white) {
                    index[i++]=w[k++];

                }
                printf ("%d\n",N);
                printf("%d ", length);
                for (i=0; i<length; i++){
                    if (i==length-1) printf("%d\n",index[i]);
                    else printf("%d ",index[i]);
                }

            }

            else {
                N=2;
                printf ("%d\n",N);
                printf("%d ",i);
                for (p=0; p<i; p++){
                    if (p==(i-1)) printf("%d\n",index[p]);
                    else printf("%d ",index[p]);
                }
                printf("%d ",length-i);

                int j1;
                j1=j;
                for (i=0; i<(black-j1);i++){
                    index[i]=b[j+1];
                    j++;
                }

                index[i]=w[white];

                for (p=0; p<=i; p++){
                    if (p==i) printf("%d\n",index[p]);
                    else printf("%d ",index[p]);
                }
            }
        }
        t--;
    }

    return 0;
}


brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Getting PE in problem 12599

Post by brianfry713 »

Insert these two lines after line 14:
memset(b, 0, sizeof(b));
memset(w, 0, sizeof(w));
Check input and AC output for thousands of problems on uDebug!

Faria049
New poster
Posts: 2
Joined: Thu Dec 12, 2013 6:54 pm

Re: Getting PE in problem 12599

Post by Faria049 »

brianfry713 wrote:Insert these two lines after line 14:
memset(b, 0, sizeof(b));
memset(w, 0, sizeof(w));
ok i got it accepted. but why was i getting PE before? what is memset() actually doing here?

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Getting PE in problem 12599

Post by brianfry713 »

Check input and AC output for thousands of problems on uDebug!

shuvokr
Learning poster
Posts: 66
Joined: Tue Oct 02, 2012 8:16 pm
Location: Bangladesh

Re: Getting PE in problem 12599

Post by shuvokr »

I got continuously PE but not understand why... Help anyone please...
My code:

Code: Select all

#include <stdio.h>
#include <string.h>
int main()
{
    int T, k, len, j, i, mark, b, w, res[ 5010 ], a, c;
    char ch[ 10010 ];
    scanf("%d", &T); getchar();
    while(T--)
    {
        mark = 1;
        gets( ch ); k = 0;
        len = strlen( ch );
        for(i = 0; i < len;)
        {
            b = w = 0;
            while(ch[ i ] == 'B')
            {
                i++;
                b++;
            }
            while(ch[ i ] == 'W')
            {
                i++;
                w++;
            }
            if(b == 0 || w == 0) mark = 0, i = len;
            else res[ k++ ] = i;
        }
        if(mark == 0) puts("IMPOSSIBLE");
        else
        {
            printf("%d\n", k);
            i = 0;
            for(j = 0; j < k; j++)
            {
                a = res[ j ] - i;
                printf("%d", a);
                b = i + 1; c = res[ j ] + 1;
                for(;b < c; b++) printf(" %d", b);
                puts(""); i = res[ j ];
            }
        }
    }
    return 0;
}

Code: Select all

enjoying life ..... 

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Getting PE in problem 12599

Post by brianfry713 »

N is always 1 or 2.
Check input and AC output for thousands of problems on uDebug!

Post Reply

Return to “Volume 125 (12500-12599)”