字符串查找算法之(一)KMP算法

问题:查找Text中是否含有Pattern字符串,返回Pattern在Text中的位置。

 

#include <string.h>
#include <iostream>

using namespace std;
// init the prefix array. when comparing, if text[i] != pattern[j], 
// then pattern[prefix[j]] should be next check point of pattern against text[i].
//
// say y = prefix[j], y stands for the biggest length of the word pattern[0..(y-1)]
// that makes (pattern[0..(y-1)] == pattern[(j-y+1)..j]). 
// In another word, y stand for the next comparation point of pattern.
// (y==0) means no such word, the next comparation should shart from pattern[0].
// Note: it's possible that 0<y<(i-y)<j and 0<(i-y)<y<j.
//
// When comparing, if (text[i] != pattern[j]) then j = prefix[j-1], 
// then compare again with text[i] until (j==0)
//
void InitPrefix(const char *pattern, int prefix[])
    {
    int len = strlen(pattern);
    prefix[0] = 0;
    for (int i = 1; i < len; i++)
        {
        int k = prefix[i - 1];
        while ((k > 0) && (pattern[i] != pattern[k]))
            {
            k = prefix[k - 1];
            }
        if (pattern[i] == pattern[k])
            {
            prefix[i] = k + 1;
            }
        else
            {
            prefix[i]=0;
            }
        }
    return;
    }

int StrStr(const char* text, const char* pattern)
    {
    if (!text || !pattern || pattern[0] == '/0' || text[0] == '/0')
        {
        return -1;
        }

    int lenText = strlen(text);
    int lenPattern = strlen(pattern);

    if (lenText < lenPattern) 
        {
        return -1;
        }

    int *prefix = new int[lenPattern + 1];
    InitPrefix(pattern, prefix); //the prefix array of pattern

    int index = -1; // the index to be returned.
    int i = 0; // the compare index in text.
    int j = 0; // the compare index in pattern
    while ((text[i] != '/0') && ((lenText - i) >= (lenPattern - j)))
        {
        if (pattern[j] == text[i])
            {
            //reach the end of pattern, find match! return.
            if (pattern[j+1] == '/0')
                {
                index = i - lenPattern + 1;
                break;
                }
            // compare next char in pattern and text.
            j++;
            i++; 
            }
        else
            {
            if (j == 0)
                {
                // pattern[0] != text[i], then skip to text[i+1].
                i++;
                }
            else 
                {
                //pattern[j]!= text[i], the next check point should be pattern[prefix[j-1]]
                j = prefix[j-1];
                }
            }
        }
    delete []prefix;
    prefix=0;
    return index;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值