首页 > 其他 > 详细

KMP revisited

时间:2015-03-25 06:28:58      阅读:131      评论:0      收藏:0      [点我收藏+]

The code below is for: http://hihocoder.com/problemset/solution/237010

With enough comments to understand:

#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;

int kmp_cnt(const string &tgt, const string &pt)
{
    size_t lent = tgt.length();
    size_t lenp =  pt.length();
    if (lenp > lent || lenp == 0) return 0;
    
    //    Pass 1
    vector<int> matched(lenp + 1, 0); //    in terms of length, as j
    
    // i: inx on target, j: matched count on pattern
    int j = 0;
    for(int i = 1; i < lenp; i ++)    
    {
        //    j > 0 below is to avoid deadloop when matched[j] is 0 too
        while (j > 0 && pt[j] != pt[i]) j = matched[j];
        //    matching? inc counter
        if (pt[j] == pt[i]) j ++;
        //    update jump table - number of chars matched
        matched[i + 1] = j;
    }

    //    Pass 2
    int ret = 0;
    for (int i = 0, j = 0; i < lent; i ++)
    {
        while (j > 0 && tgt[i] != pt[j]) j = matched[j];
        if (tgt[i] == pt[j]) j ++;
        if (j == lenp)
        {
            ret ++;
        }
    }
    return ret;
}

int main() 
{
    int t; cin >> t;
    string tmp;    getline(cin, tmp); // consume the newline from above

    while (t--)
    {
        string sp;     getline(cin, sp);
        string st;     getline(cin, st);

        int ret = kmp_cnt(st, sp);
        cout << ret << endl;
    }
    return 0;
}

KMP revisited

原文:http://www.cnblogs.com/tonix/p/4364581.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!