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

×

ZCO15001(Break Up) Error!

/*
 *            #dCoding
 */
import java.util.*;
class ZCO15001
{
    int na[],dp[],n;
    void input()
    {
        Scanner s=new Scanner(System.in);
        n=s.nextInt();
        na=new int[n];
        dp=new int[n+1];
        for(int i=0;i<n;i++){
            na[i]=s.nextInt();
        }
    }

    boolean checkPalin(int i,int j)
    {
        boolean res=true;
        for(int k=i;k<=(i+j)/2;k++){
            if(na[k]!=na[j-(k-i)]){
                res=false;
                break;
            }
        }
        return res;
    }

    void solve()
    {
        dp[0]=0;
        for(int i=1;i<=n;i++)
            dp[i]=i;
        byte flag=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<i;j++){
                if(checkPalin(j,i)){
                    dp[i+1]=Math.min(dp[i+1],dp[j]+1);
                    //System.out.println("dp["+(i+1)+"]="+dp[i+1]);
                    flag=1;
                    //break;
                }
            }
            if(flag==0){
                dp[i+1]=dp[i]+1;
            }
            else{
                flag=0;
            }
            //System.out.println("dp["+(i+1)+"]="+dp[i+1]);
        }
    }

    void display()
    {
        System.out.println(dp[n]);
    }

    public static void main(String[] args)
    {
        ZCO15001 Z=new ZCO15001();
        Z.input();
        Z.solve();
        Z.display();
    }
}

Can someone help me to find the error in my code? It passes only 3 out of the seven test cases and receives WA in the others!

asked 22 Nov '18, 18:00

dbhatt_10's gravatar image

2★dbhatt_10
923
accept rate: 0%

converted to question 23 Nov '18, 13:37

(23 Nov '18, 13:38) dbhatt_102★

Try removing the if ... else in the nested for loop and do this instead: dp[i+1] = min(dp[i+1], dp[i]+1).

(23 Nov '18, 15:09) the_extractor4★

Thanks, @the_extractor. Got my solution debugged. :)

(23 Nov '18, 19:05) dbhatt_102★
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:

×2,738
×427
×247
×74
×8

question asked: 22 Nov '18, 18:00

question was seen: 98 times

last updated: 23 Nov '18, 19:05