zoukankan      html  css  js  c++  java
  • KMP算法

    #include<iostream>
    #include <cstring>
    #include<cstdlib>
    
    using namespace std;
    
    int BF(char s[], char t[])            //BF算法
    {
        int i = 0, j = 0;
        int m = strlen(s);
        int n = strlen(t);
        while (i < m&&j < n)
        {
            if (s[i] == t[j])
            {
                i++; j++;
            }
            else
            {
                i = i - j + 1; j = 0;
            }
        }
        if (j >= n)
            return i - n + 1;
        else
            return -1;
    }
    
    void GetNext_one(char t[], int next[])  //下标为 1 开始 
    {
    
        int j = 1, k = 0;
        int n = strlen(t);
        next[j] = 0;
        while (j < n)
        {
            if (k == 0 || t[j] == t[k])
            {
                j++; k++; next[j] = k;
            } 
            else
            {
                k = next[k];
            }
        }
    }
    
    void GetNext_two(char t[], int next[])    //下标为0 
    {
    
        int j = 0, k = -1;
        int n = strlen(t);
    
        next[j] = -1;
        while (j < n)
        {
            if (k == -1 || t[j] == t[k])
            {
                j++; k++; next[j] = k;
            }
            else
            {
                k = next[k];
            }
        }
    }
    
    int IndexKMP_one(char s[], char t[], int next[], int pos)
    {
        int i, j;
        i = pos - 1; j = 0;
        int m = strlen(s),n = strlen(t);
        while (i<m && j<n)
            if (j == 0 || s[i] == t[j])
            {
                i++; j++;
            }// 继续比较后继字符
            else j = next[j];// 模式串向右移动
        if (j >= n) return i - n + 1; // 匹配成功
        return -1;
    }
    
    int IndexKMP_two(char s[], char t[], int next[], int pos)
    {
        int i, j;
        i = pos - 1; j = 0;
        int m = strlen(s),n = strlen(t);
        while (i<m && j<n)
            if (j == -1 || s[i] == t[j])
            {
            i++; j++;
            }// 继续比较后继字符
            else j = next[j];// 模式串向右移动
            if (j >= n) return i - n + 1; // 匹配成功
            return -1;
    }
    
    int main()
    {
        //char s[] = "aaabaaaabaa", t[] = "aaaab";
        char *s = "ababaaabaabcabcacabbaaababcd", *t = "abcabcacab"; //char *s = "aaaaaaabaa", *t = "abcdsb";ababaaababaa 
        int i = BF(s, t);              //abcabcacab
        cout << i << endl;
        int n = strlen(t);
        int *next, x = 0;
        next = new int[n+1];
        //next = (int *)malloc(sizeof(int)*n);
        
        GetNext_one(t, next);                 // next从 0开始 ( 1---n )
    
    //    next[1] = 0; next[2] = 1; next[3] = 1; next[4] = 1; next[5] = 2; next[6] = 3; next[7] = 4;next[8] = 5; next[9] = 1;
    //    next[10] = 2;
        for (int i = 1; i <= n; i++)
            cout << next[i] << ' ';
        cout << endl;
        
        x = IndexKMP_one(s, t, next, 1);          
    
        cout << x << endl;
        
        cout << "=================== 2 =====================
    ";
    
        GetNext_two(t, next);
        for (int i = 0; i < n; i++)          //next从 -1 开始 (0 ~ n-1) 
            cout << next[i] << ' ';
        
        cout << endl;
        x = IndexKMP_two(s, t, next, 1);
    
        cout << x << endl;
    
        system("pause");
        return 0;
    }
  • 相关阅读:
    iOS开发的一些奇巧淫技2
    iOS开发的一些奇巧淫技
    指定控制器跳转
    去掉UITableView HeaderView或FooterView随tableView 移动的黏性
    TextView随键盘弹出上移高度
    The Swift Programming Language-官方教程精译Swift(9) 枚举-- --Enumerations
    登录记住账号和密码小Demo
    The Swift Programming Language-官方教程精译Swift(8)闭包 -- Closures
    The Swift Programming Language-官方教程精译Swift(7)函数 -- Functions
    The Swift Programming Language-官方教程精译Swift(6)控制流--Control Flow
  • 原文地址:https://www.cnblogs.com/douzujun/p/9316296.html
Copyright © 2011-2022 走看看