2012-11-19 19 views
2

我已經根據site使用博耶Moore算法。這隻在文本中執行模式搜索一次,程序退出。誰能幫我,以便找到與他們的起始和結束索引模式多次修改這個代碼?修改博耶·摩爾多模式搜索

public class BoyerMoore { 
     private final int R;  // the radix 
     private int[] right;  // the bad-character skip array 
     private String pat;  // or as a string 

     // pattern provided as a string 
     public BoyerMoore(String pat) { 
      this.R = 256; 
      this.pat = pat; 

      // position of rightmost occurrence of c in the pattern 
      right = new int[R]; 
      for (int c = 0; c < R; c++) 
       right[c] = -1; 
      for (int j = 0; j < pat.length(); j++) 
       right[pat.charAt(j)] = j; 
     } 

     // return offset of first match; N if no match 
     public ArrayList<Integer> search(String txt) { 
      int M = pat.length(); 
      int N = txt.length(); 
      ArrayList<Integer> newArrayInt = new ArrayList<Integer>(); 
      int skip; 
      for (int i = 0; i <= N - M; i += skip) { 
       skip = 0; 
       for (int j = M-1; j >= 0; j--) { 
        if (pat.charAt(j) != txt.charAt(i+j)) { 
         skip = Math.max(1, j - right[txt.charAt(i+j)]); 
         break; 
        } 
       } 
       if (skip == 0) 
        newArrayInt.add(i); // found 
      } 
      return newArrayInt;      // not found 
     } 

     // test client 
     public static void main(String[] args) { 
      String pat = "abc"; 
      String txt = "asdf ghjk klll abc qwerty abc and poaslf abc"; 

      BoyerMoore boyermoore1 = new BoyerMoore(pat); 

      ArrayList<Integer> offset = boyermoore1.search(txt); 

      // print results 
      System.out.println("Offset: "+ offset); 


} 
} 
+1

你的努力顯然是在這裏失蹤。首先,你已經粘貼您還沒有寫過代碼,而不是理解,現在你要求我們修改爲你! – codaddict

+0

@codaddict,我已經更新了代碼。我試圖在ArrayList中添加模式的所有偏移索引,但每次都添加相同的索引。 – Sujan

回答

5

我明白了。在文本中找到該模式時,跳過始終爲0。

public class BoyerMoore { 
    private final int R;  // the radix 
    private int[] right;  // the bad-character skip array 
    private String pat;  // or as a string 

    // pattern provided as a string 
    public BoyerMoore(String pat) { 
     this.R = 256; 
     this.pat = pat; 

     // position of rightmost occurrence of c in the pattern 
     right = new int[R]; 
     for (int c = 0; c < R; c++) 
      right[c] = -1; 
     for (int j = 0; j < pat.length(); j++) 
      right[pat.charAt(j)] = j; 
    } 

    // return offset of first match; N if no match 
    public ArrayList<Integer> search(String txt) { 
     int M = pat.length(); 
     int N = txt.length(); 
     ArrayList<Integer> newArrayInt = new ArrayList<Integer>(); 
     int skip; 
     for (int i = 0; i <= N - M; i += skip) { 
      skip = 0; 
      for (int j = M-1; j >= 0; j--) { 
       if (pat.charAt(j) != txt.charAt(i+j)) { 
        skip = Math.max(1, j - right[txt.charAt(i+j)]); 
        break; 
       } 
      } 
      if (skip == 0) 
      { 
       newArrayInt.add(i); // found 
       skip++; 
      } 
     } 
     return newArrayInt;      // not found 
    } 

    // test client 
    public static void main(String[] args) { 
     String pat = "abc"; 
     String txt = "asdf ghjk klll abc qwerty abc and poaslf abc"; 

     BoyerMoore boyermoore1 = new BoyerMoore(pat); 

     ArrayList<Integer> offset = boyermoore1.search(txt); 

     // print results 
     System.out.println("Offset: "+ offset); 
    } 
} 
0
public class Boyer { 
    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     Scanner get= new Scanner(System.in); 
     String m,n; 
     int i,j; 
     String T,P; 
     T=get.nextLine(); 
     System.out.println("Text T is"+T); 
     P=get.nextLine(); 
     System.out.println("Pattern P is"+P); 
     int n1=T.length(); 
     int m1=P.length(); 
     for(i=0;i<=n1-m1;i++){ 
      j=0; 
      while(j<m1 && (T.charAt(i+j)==P.charAt(j))){ 
       j=j+1; 
       if(j==m1) 
        System.out.println("match found at"+i); 
      } 
     } 
    } 
}