• KMP算法实现


    字符串匹配问题  

      给定一个主串(以 S 代替)和模式串(以 P 代替),要求找出 P 在 S 中出现的位置,此即串的模式匹配问题。

    解决这种类型的问题的两种方法


      1、常规暴力解法
      2、KMP算法(及优化方法)

    C++实现

      1 #include <stdio.h>
      2 #include <iostream>
      3 #include <algorithm>
      4 #include <vector>
      5 using namespace std;
      6 
      7 /**
      8  * KMP:
      9  * 给定一个主串(以 S 代替)和模式串(以 P 代替),要求找出 P 在 S 中出现的位置,此即串的模式匹配问题。
     10  */
     11 
     12 class Solution {
     13 
     14 private:
     15 
     16 public:
     17 
     18     /**
     19      * 暴力解法
     20      * @param s :主串
     21      * @param p :模式字符串
     22      * @return
     23      */
     24     int bruteForce(string s, string p) {
     25         int i = 0;
     26         int j = 0;
     27         int lengthOfS = s.size();
     28         int lengthOfP = p.size();
     29 
     30         while (i < lengthOfS && j < lengthOfP) {
     31             if (s[i] == p[j]){
     32                 i++;
     33                 j++;
     34             }else {
     35                 i = i-j+1;
     36                 j = 0;
     37             }
     38         }
     39 
     40         if (j == lengthOfP) {
     41             return i-j;
     42         }
     43 
     44         return -1;
     45     }
     46 
     47     /**
     48      * 求解模式串的每个位置的相同真前后缀
     49      * @param pattern:模式字符串
     50      * @param next  :模式串的每个位置的相同真前后缀长度
     51      */
     52     int * getNext(string pattern) {
     53         int* next = new int[100]{0};
     54         int patternSize = pattern.size();
     55         int i = 0,j = -1;
     56         next[0] = -1;
     57 
     58         while(i < patternSize) {
     59             if (j == -1 || pattern[i] ==  pattern[j]) {
     60                 i++;
     61                 j++;
     62                 next[i] = j;
     63             }else {
     64                 j = next[j];
     65             }
     66         }
     67         return next;
     68     }
     69 
     70     /**
     71      * KMP implementation
     72      * @param s
     73      * @param p
     74      * @param next
     75      * @return
     76      */
     77     int KMP(string s, string p) {
     78         int* next = getNext(p);
     79         int i = 0;
     80         int j = 0;
     81         int lengthOfS = s.size();
     82         int lengthOfP = p.size();
     83 
     84         while(i < lengthOfS && j < lengthOfP) {
     85             if(j == -1 || s[i] == p[j]) {   // P 的第一个字符不匹配或 S[i] == P[j]
     86                 i++;
     87                 j++;
     88             }else {
     89                 j = next[j];
     90             }
     91         }
     92 
     93         if (j == lengthOfP) {
     94             return i-j;
     95         }
     96         return -1;
     97     }
     98 
     99 };
    100 
    101 int main() {
    102     string s,p;
    103     getline(cin,s);
    104     getline(cin,p);
    105     Solution newSolution;
    106     cout << newSolution.KMP(s,p) << endl;
    107 
    108 }
    专注搬砖,擅长搬砖砸自己的脚~~~ Email: ltwbuaa@163.com
  • 相关阅读:
    程序员7种武器成
    FastReport 使用技巧篇
    FastReport调用Delphi中的自定义函数(人民币大写金额)mtm
    FastReport里面正确调用函数的方法
    FastReport报表设计
    FastReport报表对象介绍一:“Text”对象
    设定报表变量的CharSpacing
    FastReport问题整理(技巧)
    FASTREPORT 整理 (mtm)
    fastReport 运行时设计报表 (mtm)
  • 原文地址:https://www.cnblogs.com/TonvyLeeBlogs/p/9472899.html
Copyright © 2020-2023  润新知