码迷,mamicode.com
首页 > 其他好文 > 详细

KMP

时间:2015-04-06 17:08:20      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

#include<iostream>
#include<string>
#include <math.h>
#include <assert.h>
#include <string.h>
using namespace std;
//朴素模式匹配
void Naive_String_Matcher(string T, string P)
{
    int n = T.length();
    int m = P.length();
    for (int s = 0; s <= n - m; s++)
    {
        int j;
        for (j = 0; j < m; j++)
        {
            if (P.at(j) != T.at(j + s))
            {
                break;
            }
        }
        if (j==m)
        {
            cout << "Pattern occurs at  " << s << endl;
        }
    }
}



//Rabin_Karp 模式匹配算法
/*
*T:文本串
*P:模式串
*d:进制
*q:一个素数
*/
void Rabin_Karp_Matcher(string T, string P, int d, int q)
{
    int n = T.length();
    int m = P.length();
    int p = 0;
    int t = 0;

    int h=1;
    //(d^(m-1))mod q
    for (int i = 0; i < m - 1; i++)
    {
        h = (h*d)%q;
    }
    for (int i = 0; i < m; i++)
    {
        p = (d*p+P[i])%q;
        t = (d*t + T[i]) % q;
    }
    for (int s = 0; s <= n - m; s++)
    {
        if (p == t)
        {
            int j;
            for (j = 0; j < m; j++)
            {
                if (P.at(j) != T.at(j+s))
                    break;
            }
            if (j == m)
            {
                cout << "Macther at " << s << endl;
            }
        }
        if (s < n - m)
        {
            t = (d*(t - T[s] * h) + T[s + m] )% q;
            if (t < 0)
                t += q;
        }
            
    }

}
//计算模式串的前缀函数
/*
*比如P="abab"
*t[]={0,0,1,2}
*t存放的是回退的位置
*/
int *Compute_Prefix_Function(string P)
{
    int m = P.length();
    int *t = new int[m];
    t[0] = 0;
    int k = 0;
    for (int q = 1; q < m; q++)
    {
        while (k>0 && P[k] != P[q])
            k = t[k-1];
        if (P[k] == P[q])
            k = k + 1;
        t[q] = k;
    }

    return t;

}
void KMP_Matcher(string T, string P)
{
    int n = T.length();
    int m = P.length();
    int *t = Compute_Prefix_Function(P);
    for (int i = 0; i < m; i++)
    {
        cout << t[i] << endl;
    }
    int q = 0;
    for (int i = 0; i < n; i++)
    {
        while (q>0 && P[q] != T[i])
        {
            q = t[q-1];
        }
            
        if (P[q] == T[i])
            q = q + 1;
        if (q == m)
        {
            cout << "Matcher at " << i - m+1 << endl;
            q = t[q-1];
        }
    }
    delete[]t;
}



int main()
{
    //string T = "acaabcaab";
    //string P = "aab";
    //Naive_String_Matcher(T,P);
    //cout << "---------------------" << endl;
    //Rabin_Karp_Matcher(T,P,256,127);
    //cout << "---------------------" << endl;
    string P = "abab";
    string T = "ababababbc";
    Compute_Prefix_Function(P);
    KMP_Matcher(T, P);
    return 0;
}

 

 

KMP

标签:

原文地址:http://www.cnblogs.com/liuhg/p/KMP.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!