Java实现算法导论中朴素字符串匹配算法

来源:互联网 发布:淘宝店铺首页海报制作 编辑:程序博客网 时间:2024/05/08 16:37

朴素字符串匹配算法沿着主串滑动子串来循环匹配,算法时间性能是O((n-m+1)m),n是主串长度,m是字串长度,结合算法导论中来理解,具体代码参考:

package cn.ansj;public class NavieStringMatcher {public static void main(String[] args) {          String T = "欢迎访问fjssharpsword博客,致力于大数据应用解决方案提供!";          String P = "大数据应用解决方案";          NavieStringMatcher  nsm = new NavieStringMatcher();          int index = nsm.stringMatcher(T, P);          System.out.println("有效位移是: "+index);      }      /**      * @author fjssharpsword       * @param T 主字符串      * @param P 模式字符串      * @return s 有效位移     */      public int stringMatcher(String T, String P){          int iTLen = T.length();          int iPLen = P.length();          // 从主串开始比较          for(int i=0; i<iTLen; i++) {              int k = i; // k指向主串下一个位置              for(int j=0; j<iPLen; j++) {                  if(T.charAt(k) != P.charAt(j)) {                      break;                  }else {                      k++;// 指向主串下一个位置                      if(j == iPLen-1) {                          return i;                      }                  }                                 }          }                  return -1;  // 匹配不成功,返回-1     }  }
执行结果:

有效位移是: 23



0 0
原创粉丝点击