zoukankan      html  css  js  c++  java
  • Kmp 模板(邝斌

    1,求第一次匹配的位置

    void getNext(int m){
        int i=0,j=-1;
        Next[0]=-1;
    
    
        while(i < m){
            if(j == -1 || p[i] == p[j])
                Next[++i] = ++j;
            else
                j=Next[j];
        }
    }
    
    
    bool Kmp(int n,int m){
        getNext(m);
        int i = 0,j = 0;
        while(i < n && j < m){
            if(j == -1 || s[i] == p[j]){
                i++;
                j++;
            }
            else
                j=Next[j];
        }
        if(j >= m)  return true;
        else
            return false;
    }


    2,求主串中模式串匹配的次数

    void getNext(int m){
        Next[0]=-1;
        int i = 0,j = -1;
        while(i < m){
            if(j == -1 || p[i] == p[j])
                Next[++i]=++j;
            else
                j = Next[j];
        }
    }
    int Kmp(int n,int m){
        getNext(m);
        int i = 0,j = 0;
        int ans = 0;
        while(i < n && j < m){
            if(j == -1 || s[i] == p[j]){
                i++;
                j++;
            }
            else
                j = Next[j];
            if(j >= m){
                ans++;
                j = Next[j];
            }
        }
        return ans;
    }


    3,求 Next 数组的时候

    Next[++i]=++j;
    
    总是会忘了 ++i,和++j。一定注意别忘了 ++。

  • 相关阅读:
    DVD X Player 5.5 PRO
    Freefloat FTP Server 1.0漏洞分析
    基于约束的SQL攻击
    Commons-Collections漏洞
    Code-Audit-Challenges-php-2
    GSM Sniffer环境--c118+osmocombb
    XXE (XML External Entity Injection) :XML外部实体注入
    hyperledger fabric学习(1)
    zero to one (4)
    zero to one (3)
  • 原文地址:https://www.cnblogs.com/Jstyle-continue/p/6351964.html
Copyright © 2011-2022 走看看