【问题标题】:Modifying Boyer Moore for multiple pattern search修改 Boyer Moore 以进行多模式搜索
【发布时间】:2012-11-19 05:56:43
【问题描述】:

我根据site 使用了 Boyer 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);


 }
}

【问题讨论】:

  • 你的努力显然在这里丢失了。首先,您粘贴了一个您没有编写且不理解的代码,现在您要求我们为您修改它!!!
  • @codaddict,我已经更新了代码。我试图在 ArrayList 中添加模式的所有偏移索引,但每次都添加相同的索引。

标签: java algorithm boyer-moore


【解决方案1】:

我明白了。当它在文本中找到模式时,skip 始终为 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);
    }
}

【讨论】:

    【解决方案2】:
    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);
              }
          }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-18
      • 2020-10-13
      • 2020-07-17
      • 2012-07-12
      • 1970-01-01
      • 2010-10-28
      相关资源
      最近更新 更多