2016-12-23 4 views
1
public class App { 

    public static int min(int x, int y) 
    { 
     if(x<y) 
      return x; 
     else 
      return y; 
    } 

    public static int minPalPartition(String str) 
    { 
     int n = str.length(); 
     boolean P[][] = new boolean[n][n]; 
     int DP[][] = new int[n][n]; 

     //for all the string with start index and end index is same that is length is 1, string is palindrome and cut needed is 0 
     for(int i=0; i<n; i++) 
     { 
      P[i][i] = true; 
      DP[i][i] = 0; 
     } 

     /* 
     * i start index 
     * j end index 
     * k intermediate index 
     * l length 
     */ 
     int i, j, k, l; 

     for(l=2; l<=n; l++) 
     { 
      for(i=0; i<n-1; i++) //as starting index start at 0 it can go till n-2, n=3, i =0, j=1 and i=1, j=2 is the combination 
      { 
       j=i+l-1; 

       /* first determine P[i][j], if P[i][j] is true then DP[i][j] is 0 
       * if only 2 letter just check first and last letter 
       * otherwise last 2 letter and previous result 
       */ 
       if(l==2) 
       { 
        P[i][j] = (str.charAt(i) == str.charAt(j)); 

       } 
       else 
       { 

        P[i][j] = (str.charAt(i)== str.charAt(j)) && P[i+1][j-1]; 

       } 

       if(P[i][j] == true) 
       { 
        DP[i][j] = 0; 
       } 
       else 
       { 
        DP[i][j] = Integer.MAX_VALUE; 
        for(k=i; k<j; k++) 
        { 
         DP[i][j] = min(DP[i][j], (DP[i][k] + DP[k+1][j] + 1)); 
        } 
       } 
      } 
     } 

     return DP[0][n-1]; 
    } 

    public static void main(String[] args) { 
     String str = "ababbbabbababa"; 
     System.out.println("length of the string " + str.length()); 
     System.out.println("pal partition Need for [" + str + "] : " + minPalPartition(str)); 

    } 

} 

In dem obigen Code ich unter Ausnahme bekamJava-String gebundener Ausnahme, während das Element Array Zugriff auf

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 14 
    at java.lang.String.charAt(String.java:658) 
    at Client.App.minPalPartition(App.java:54) 
    at Client.App.main(App.java:79) 

Grundsätzlich gibt es Ausnahme an dieser Linie

P[i][j] = (str.charAt(i)== str.charAt(j)) && P[i+1][j-1]; 

Was ist das Problem ? Wie man es vermeidet.

Dies ist Palindrome Partitionierung eines String-Problems.

+0

überprüfen Sie immer, bevor Sie auf ein bestimmtes Element eines Strings zugreifen. Wenn der Index größer als die Größe der Zeichenfolge ist, wird es Ihnen "StringIndexOutOfBoundsException" geben. –

+1

'j = i + l-1;' wird größer als oder gleich "n". Beispiel wenn 'l == 3' und' i == n - 2' also 'str.charAt (j)' die Ausnahme auslöst. –

+0

hier n = 14, l = 2, i = n-2 = 12 –

Antwort

1

Wie bereits erwähnt in dem Kommentar, dass Sie ein Problem in den folgenden Codezeilen haben.

for (l = 2; l <= n; l++){ 
    for (i = 0; i < n - 1; i++){ 
     j = i + l - 1; 
     // rest of your code 
    } 
} 

Also, Ihre Codierung hebt Ausnahme, wenn l = 3 und i = 12, weil j dann 12 + 3 - 1 = 14 wird. Da Ihre String-Länge 14 ist, können Sie nicht auf Index 14 zugreifen und als Ergebnis StringIndexOutOfBoundsException Ausnahme erhalten.

Aus den Kommentaren Sie in Ihrem Code gemacht, ich denke, was Sie brauchen, ist:

for (l = 2; l <= n; l++){ 
    for (i = 0; i < n - l; i++){ 
     j = i + l - 1; 
     // rest of your code 
    } 
} 

Hinweis der Zustand der inneren Schleife, seine i < n - l, nicht i < n - 1. So kann Maximalwert von j sein:

j = n - l + l - 1 = n - 1 

Ich weiß nicht, was Sie versuchen, mit Ihrem Code zu tun, aber ich habe dies auf der Grundlage meiner Vermutung vorgeschlagen, nachdem Ihre Kommentare im Code zu lesen.

0
j=i+l-1; 
    //l=[2,n] 
    //i=[0,n-2] 
when l=3 i=n-2,j=n,then out of bounds 
Verwandte Themen