• kmp算法 汇总


    来源:http://blog.csdn.net/qq_34494458/article/details/75253466

    KMP算法,是由Knuth,Morris,Pratt共同提出的模式匹配算法,其对于任何模式和目标序列,都可以在线性时间内完成匹配查找,而不会发生退化,是一个非常优秀的模式匹配算法。

    /*
     * next[]的含义(前提):x[i-next[i]...i-1] = x[0...next[i]-1]这很重要;
     * next[i]为满足x[i-z...i-1] = x[0...z-1]的最大值z(就是x的自身匹配);
     */
     
     //求next的代码实现
     /*
     * next[]求到了next[m],这个next[m]作用还很大;
     */
    void kmp_pre(char x[], int m, int next[]) {
        int i, j;
        j = next[0] = -1;
        i = 0;
        while (i < m) {
            while (-1 != j && x[i] != x[j]) j = next[j];
            //j = -1,表示第0位都没匹配成功;那就要直接推进一位;
            next[++i] = ++j;
        }
    }
    
    /*
     *还可以有一个小优化;
     */
    void preKMP(char x[], int m, int kmpNext[]) {
        int i, j;
        j = kmpNext[0] = -1;
        i = 0;
        while (i < m) {
            while (-1 != j && x[i] != x[j]) j = kmpNext[j];
            if (x[++i] == x[++j]) kmpNext[i] = kmpNext[j];
            else kmpNext[i] = j;
            /*这个if很6,这除去了一些无意义的next[],大概意思是
            *如果x[j]匹配失败了,那么就执行 j = next[j];
            *而x[j] = x[next[j]]所以x[next[j]]肯定也会匹配失败。
            *所以就说这个next[j]是无意义的。
            */
        }
    }
    
    /*
     *x与y匹配;
     *返回x在y中出现的次数,可以重叠
     *与求next[]函数的写法基本相似;
     */
     
     int next[10010];
     int KMP_Count(char x[], int m, char y[], int n) {
         //x是模式串,y是主串;
         int i, j;
         int ans = 0;
         //preKMP(x, m, next);
         kmp_pre(x, m, next);
         i = j = 0;
         while (i < n) {
            while (-1 != j && y[i] != x[j]) j = next[j];
            i++; j++;
            if (j >= m) {
                ans++;
                j = next[j];
            }
         }
         return ans;
     }

    经典题目:

    加一个题目:http://poj.org/problem?id=3167
    /*
     *模式串可以浮动的模式串匹配问题
     *给出模式串的相对大小,需要找出模式串匹配次数和位置
     *比如说模式串: 1,4,4,2,3,1 而主串:5,6,2,10,10,7,3,2,9
     *那么子串:2,10,10,7,3,2就是和模式串匹配的。
     *思路:只需比较前面比当前数小的数与等于当前数的数的个数就好了,看这两个东西是否相等来进行kmp。
     */
     //#include<bits/stdc++.h>
    #include <iostream>
    #include <string>
    #include <queue>
    #include <map>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    using namespace std;
    typedef long long LL;
    #define lson k<<1, ll, mid
    #define rson k<<1|1, mid+1, rr
    const int MAXN = 100008;
    int n, k, s, next[MAXN>>2], as[MAXN][26], bs[MAXN>>2][26], a[MAXN], b[MAXN];
    vector<int> ans;
    
    void init() {//把输入的字符串同化成as和bs;
        scanf("%d%d%d", &n, &k, &s);
        for(int i = 0; i < n; i++) {
            scanf("%d", &a[i]);
            if (i != 0) {
                for(int j = 1; j < 26; j++)
                    as[i][j] = as[i-1][j];
            }
            as[i][a[i]]++;
        }
        for(int i = 0; i < k; i++) {
            scanf("%d", &b[i]);
            if (i != 0) {
                for(int j = 1; j < 26; j++)
                    bs[i][j] = bs[i-1][j];
            }
            bs[i][b[i]]++;
        }
    }
    
    //这里是没有嵌套while循环的写法,都是一样的。
    void build_next() {
        next[0] = -1;
        next[1] = 0;//这里皮了一下
        int j = 0, i = 1;
        while (i < k) {
            int t11 = 0, t12, t21 = 0, t22;
            for(int t = 1; t < b[i]; t++)
                if (i == j) t11 += bs[i][t];
                else t11 += (bs[i][t]-bs[i-j-1][t]);
            if (i == j) t12 = bs[i][b[i]];
            else t12 = bs[i][b[i]]-bs[i-j-1][b[i]];
            for(int t = 1; t < b[j]; t++)
                t21 += bs[j][t];
            t22 = bs[j][b[j]];
            if (t11 == t21 && t12 == t22)
                next[++i] = ++j;
            else j = next[j];
        }
    }
    
    void kmp() {
        ans.clear();
        build_next();
        int i = 0, j = 0;
        while (i < n) {
            int t11 = 0, t12, t21 = 0, t22;
            for(int t = 1; t < a[i]; t++)
                if (i == j) t11 += as[i][t];
                else t11 += (as[i][t]-as[i-j-1][t]);
            if (i == j) t12 = as[i][a[i]];
            else t12 = as[i][a[i]]-as[i-j-1][a[i]];
            for(int t = 1; t < b[j]; t++)
                t21 += bs[j][t];
            t22 = bs[j][b[j]];
            if (t11 == t21 && t12 == t22) {
                ++i; ++j;
                if (j >= k) {
                    ans.push_back(i-j+1);
                    j = next[j];
                }
            }
            else j = next[j];
        }
    }
    
    int main() {
        //freopen("in.txt", "r", stdin);
        init();
        kmp();
        printf("%d
    ", s = ans.size());
        for(int i = 0; i < s; i++)
            printf("%d
    ", ans[i]);
        return 0;
    }
  • 相关阅读:
    转:彻底搞清楚javascript中的require、import和export
    转:博客园新随笔 添加锚点
    转:深入浅出空间索引:为什么需要空间索引
    转:常见的空间索引方法
    可视化&地图__公司收集
    js json转xml(可自定义属性,区分大小写)
    Python3.6之给指定用户发送微信消息
    微信服务号发送模板消息
    log4j封装方法,输出集合
    Java封装servlet发送请求(二)
  • 原文地址:https://www.cnblogs.com/pach/p/7199132.html
Copyright © 2020-2023  润新知