2013-03-25 37 views
1

這是一個家庭作業問題。我有一個Java程序來計算某個模式/字符串是否在用戶輸入的文本字符串中。該程序工作,但總是輸出一個-1,這是它應該輸出的模式字符串不在指定的文本中。我無法弄清楚什麼是不工作的我的生活,我會對我需要解決的問題提供一點啓示。Horspool輸出錯誤

這裏是我的代碼:

import java.util.Scanner; 

/** 
* @author Mouse 
* 
*/ 
public class horspool { 

/** 
* @param args 
*/ 
public static void main(String[] args) 
{ 
    Scanner scanIn = new Scanner (System.in); 

    //The text to search for the phrase in 
    String t = ""; 

    //The phrase/pattern to search for 
    String p = ""; 

    System.out.println(" "); 
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 
    System.out.println("Harspool's Algorithm: "); 
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 
    System.out.println(" "); 
    System.out.println("Please enter the full text: "); 
    t = scanIn.nextLine(); 
    System.out.println("Please enter the pattern to search for: "); 
    p = scanIn.nextLine(); 

    char[] text = new char[t.length()]; 
    char[] pattern = new char[p.length()]; 

    for (int i = 0; i < text.length; i++) 
    { 
     text[i] = t.charAt(i); 
    } 

    for (int i = 0; i < pattern.length; i++) 
    { 
     pattern[i] = p.charAt(i); 
    } 

    int newChar[] = new int[256]; 

    for(int i=0; i < 256; i++) 
    { 
     newChar[i]=0; 
    } 

    for (int i = 0; i < text.length; i++) 
    { 
     newChar[t.charAt(i) % 256]++; 
    } 

    for (int i = 0; i < pattern.length; i++) 
    { 
     newChar[p.charAt(i) % 256]++; 
    } 

    int index = HorspoolMatching(pattern, text); 

    System.out.println("Index: " + index); 
} 

public static int[] ShiftTable(char[] p) 
{ 
    int m = p.length; 

    //Table filled with shift sizes for each individual letter. 
    int[] table = new int[256]; 

    for (int i = 0; i < table.length; i++) 
    { 
     table[i] = m; 
    } 
    for (int j = 0; j < m - 1; j++) 
    { 
     for (int a = 0; a < p.length; a++) 
     { 
      if (p[j] == p[a]) 
      { 
       table[a] = (m - 1 - j); 
      } 
     } 
    } 
    return table; 
} 


public static int HorspoolMatching(char[] p, char[] t) 
{ 
    int [] table = ShiftTable(p); 
    int m = p.length; 
    int i = m - 1; 
    int n = t.length; 
    int temp = 0; 
    int k = 0; 
    int count = 0; 

    while (i <= n - 1) 
    { 
     k = 0; 

     while (t[i - k] == p[m - 1 - k] && k < m - 1) 
     { 
      System.out.println("In second while"); 
      k++; 
     } 
     if (k == m) 
     { 
      return (i - m + 1); 
     } 
     else 
     { 
      for (int a = 0; a < t.length; a++) 
      { 
       if (t[i] == t[a]) 
       { 
        temp = table[a]; 
       } 
      } 
      i = i + temp; 
     } 
    } 
    return -1; 
} 

} 

任何幫助將不勝感激!非常感謝!

+0

使用調試器,並通過一個小例子。此外,「程序起作用」,但「不起作用」並不合理。 – 2013-03-25 18:30:51

回答

1

它總是輸出-1

入住HorspoolMatching return語句。

編輯:好的,我被騙了,對不起。

當我改變

while (t[i - k] == p[m - 1 - k] && k < m - 1) 

while (k < m && t[i - k] == p[m - 1 - k]) 

它開始工作。

問題k < m-1問題在於你太快停止一個循環,從不真正檢查整個模式,因此,沒有匹配。

通過將其移動到& &的首要條件和去除-1,它現在檢查完整的圖案,當它應該的,而將失敗:索引超出界限之前,而不是前一個循環那。

更多編輯:

我跳過現在並設置SYS:

String t = "lklklklkabcdabababcd"; 
String p = "ab"; 

其中給出8

String t = "lklklklkabcdabababcd"; 
String p = "abc"; 

是給-1。 ..

好吧,這應該做[擾流警報]

table[i] = 1; //m; 
+0

我有,但在方法中有另一個return語句: if(k == m) return(i - m + 1); } 問題是它永遠不會進入,如果 – Mouse 2013-03-25 19:01:49

+0

謝謝!這實際上更有意義。奇怪的是,它是如此小的東西。我會花一些時間努力修復你提到的-1錯誤。 – Mouse 2013-03-25 22:44:50