zoukankan      html  css  js  c++  java
  • kmp-算法

    本帖主要内容转自:https://www.61mon.com/index.php/archives/183/

    next函数

    void getNext(string P, int next[])
    {
        int p_len = P.size();
        int i = 0;   // P 的下标
        int j = -1;  
        next[0] = -1;
    
        while (i < p_len)
        {
            if (j == -1 || P[i] == P[j])
            {
                i++;
                j++;
                next[i] = j;
            }
            else
                j = next[j];
        }
    }
    

    优化的next函数

    /* P 为模式串,下标从 0 开始 */
    void GetNextval(string P, int nextval[])
    {
        int p_len = P.size();
        int i = 0;   // P 的下标
        int j = -1;  
        nextval[0] = -1;
    
        while (i < p_len)
        {
            if (j == -1 || P[i] == P[j])
            {
                i++;
                j++;
    
                if (P[i] != P[j])
                    nextval[i] = j;
                else
                    nextval[i] = nextval[j];  // 既然相同就继续往前找真前缀
            }
            else
                j = nextval[j];
        }
    }
    

      

    Kmp算法

    int KMP(string S, string P, int next[])
    {
        GetNext(P, next);
    
        int i = 0;  // S 的下标
        int j = 0;  // P 的下标
        int s_len = S.size();
        int p_len = P.size();
    
        while (i < s_len && j < p_len)
        {
            if (j == -1 || S[i] == P[j])  // P 的第一个字符不匹配或 S[i] == P[j]
            {
                i++;
                j++;
            }
            else
                j = next[j];  // 当前字符匹配失败,进行跳转
        }
    
        if (j == p_len)  // 匹配成功
            return i - j;
    
        return -1;
    }
    

      

      

  • 相关阅读:
    Java.io.outputstream.PrintStream:打印流
    Codeforces 732F. Tourist Reform (Tarjan缩点)
    退役了
    POJ 3281 Dining (最大流)
    Light oj 1233
    Light oj 1125
    HDU 5521 Meeting (最短路)
    Light oj 1095
    Light oj 1044
    HDU 3549 Flow Problem (dinic模版 && isap模版)
  • 原文地址:https://www.cnblogs.com/lvcoding/p/7594540.html
Copyright © 2011-2022 走看看