You are not logged in. Please login at www.codechef.com to post your questions!

×

need help with spoj problem broken

problem link

char ip[MAX]; int main() { int m,hash[400]; int max=-1; int l=0,r=0; scanf("%d",&m); while(m!=0) { memset(hash,0,sizeof(hash)); scanf(" %[^\n]s",ip); strcat(ip,"0"); int len=strlen(ip); l=r=0; max=-1; int dist=0; while(l<len) { while(r<len && dist<=m) { if(hash[(int)ip[r]]==0) dist++; hash[(int)ip[r]]++; r++; }

        int temp=r-l-1;
        if(temp>max)
            max=temp;
        if(hash[(int)ip[l]]<=1)
            dist--;
        hash[(int)ip[l]]--;
        l++;
    }
    int temp=r-l-1;
    if(temp>max)
        max=temp;
    printf("%d\n",max);
    scanf("%d",&m);
}

}

The code is not considering the last character of the input. So I added a "0" at the end. Now this sentinel value is not being read, giving me Correct Answer. My question is how to fix the code so that the sentinel value is not required? It definitely is causing my code to run a bit slow (though AC).

Any pointers??

asked 21 Sep '15, 13:50

dragonemperor's gravatar image

3★dragonemperor
89321134
accept rate: 10%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,134

question asked: 21 Sep '15, 13:50

question was seen: 320 times

last updated: 21 Sep '15, 13:50