类 KMP


  • public class KMP
    extends java.lang.Object
    The KMP class finds the first occurrence of a pattern string in a text string.

    This implementation uses a version of the Knuth-Morris-Pratt substring search algorithm. The version takes time proportional to n + m R in the worst case, where n is the length of the text string, m is the length of the pattern, and R is the alphabet size. It uses extra space proportional to m R.

    For additional documentation, see Section 5.3 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

    • 构造器概要

      构造器 
      构造器 说明
      KMP​(char[] pattern, int R)
      Preprocesses the pattern string.
      KMP​(java.lang.String pat)
      Preprocesses the pattern string.
    • 方法概要

      修饰符和类型 方法 说明
      static void main​(java.lang.String[] args)
      Takes a pattern string and an input string as command-line arguments; searches for the pattern string in the text string; and prints the first occurrence of the pattern string in the text string.
      int search​(char[] text)
      Returns the index of the first occurrrence of the pattern string in the text string.
      int search​(java.lang.String txt)
      Returns the index of the first occurrrence of the pattern string in the text string.
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 构造器详细资料

      • KMP

        public KMP​(java.lang.String pat)
        Preprocesses the pattern string.
        参数:
        pat - the pattern string
      • KMP

        public KMP​(char[] pattern,
                   int R)
        Preprocesses the pattern string.
        参数:
        pattern - the pattern string
        R - the alphabet size
    • 方法详细资料

      • search

        public int search​(java.lang.String txt)
        Returns the index of the first occurrrence of the pattern string in the text string.
        参数:
        txt - the text string
        返回:
        the index of the first occurrence of the pattern string in the text string; N if no such match
      • search

        public int search​(char[] text)
        Returns the index of the first occurrrence of the pattern string in the text string.
        参数:
        text - the text string
        返回:
        the index of the first occurrence of the pattern string in the text string; N if no such match
      • main

        public static void main​(java.lang.String[] args)
        Takes a pattern string and an input string as command-line arguments; searches for the pattern string in the text string; and prints the first occurrence of the pattern string in the text string.
        参数:
        args - the command-line arguments