使用三种方法在文本中搜索模式

时间:2016-11-27 05:13:10

标签: java search design-patterns

我想在给定的文本中编写一个用于模式搜索的程序,该文本读取文本,然后是一个或多个模式,并为每个模式提供: 如果找到(打印出文本中第一次出现的位置) 如果不是方法的比较数:暴力 Boyer-Moore启发式 KMP
但我不知道如何编写我的主类来获得输出:

import java.util.HashMap;
import java.util.Map;


public class PatternSearch {


/** Returns the lowest index at which substring pattern begins in text (or else -1).*/
 public static int findBrute(char[] text, char[] pattern) { 
 int n = text.length;
 int m = pattern.length;
 for (int i=0; i <= n - m; i++) { // try every starting index within text
 int k = 0; // k is index into pattern
 while (k < m && text[i+k] == pattern[k]) // kth character of pattern matches
 k++;
 if (k == m) // if we reach the end of the pattern,
 return i; // substring text[i..i+m-1] is a match
 } 
    return -1; // search failed
 }

 /** Returns the lowest index at which substring pattern begins in text (or else -1).*/
 public static int findBoyerMoore(char[] text, char[] pattern) {
     int n = text.length;
 int m = pattern.length;
 if (m == 0) return 0; // trivial search for empty string
 Map<Character,Integer> last = new HashMap<>( ); // the 'last' map
 for (int i=0; i < n; i++)
 last.put(text[i], -1); // set -1 as default for all text characters
 for (int k=0; k < m; k++)
 last.put(pattern[k], k); // rightmost occurrence in pattern is last
 // start with the end of the pattern aligned at index m-1 of the text
 int i = m-1; // an index into the text
 int k = m-1; // an index into the pattern
 while (i < n) { 
      if (text[i] == pattern[k]) { // a matching character
 if (k == 0) return i; // entire pattern has been found
 i--; // otherwise, examine previous
 k--; // characters of text/pattern
 } else { 
      i += m - Math.min(k, 1 + last.get(text[i])); // case analysis for jump step
 k = m - 1; // restart at end of pattern
 } 
  } 
 return -1; // pattern was never found
 }


 /** Returns the lowest index at which substring pattern begins in text (or else -1).*/
 public static int findKMP(char[] text, char[] pattern) { 
 int n = text.length;
 int m = pattern.length;
 if (m == 0) return 0; // trivial search for empty string
 int[] fail = computeFailKMP(pattern); // computed by private utility
 int j = 0; // index into text
 int k = 0; // index into pattern
 while (j < n) { 
 if (text[j] == pattern[k]) { // pattern[0..k] matched thus far
 if (k == m - 1) return j - m + 1; // match is complete
 j++; // otherwise, try to extend match
 k++;
 } else if (k > 0)
 k = fail[k-1]; // reuse suffix of P[0..k-1]
 else
 j++;
 } 
 return -1; // reached end without match
 }


 private static int[] computeFailKMP(char[] pattern) { 
 int m = pattern.length;
 int[ ] fail = new int[m]; // by default, all overlaps are zero
 int j = 1;
 int k = 0;
 while (j < m) { // compute fail[j] during this pass, if nonzero
 if (pattern[j] == pattern[k]) { // k + 1 characters match thus far
 fail[j] = k + 1;
 j++;
 k++;
  } else if (k > 0) // k follows a matching prefix
  k = fail[k-1];
  else // no match found starting at j
  j++;
  } 
  return fail;
  }

public static void main(String[] args) {



}

}

1 个答案:

答案 0 :(得分:0)

看起来您正在从c \ c ++迁移到Java。

以下是如何通过main方法调用函数。

    public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("1) Please enter a string :");
    String text = br.readLine();
    System.out.print("2) Please enter a pattern :");
    String pattern = br.readLine();

    System.out.println(PatternSearch.findBoyerMoore(text.toCharArray(), pattern.toCharArray()));
    System.out.println(PatternSearch.findBrute(text.toCharArray(), pattern.toCharArray()));
    System.out.println(PatternSearch.findKMP(text.toCharArray(), pattern.toCharArray()));
}

由于你的所有方法都是静态的,我使用className.method();

调用方法

如果您的方法是非静态的,那么您将必须创建该类的实例,然后使用您创建的实例调用该方法。

PatternSearch instance = new PatternSearch();
instance.findKMP(text.toCharArray(), pattern.toCharArray());