zoukankan      html  css  js  c++  java
  • KMP revisited

    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;
    }
  • 相关阅读:
    JDK15视频会及新特性总节
    设计模式之访问者模式
    datax分析与思考(一)
    beanfactory中单例bean的初始化过程(一)
    IIS 404错误,错误代码:0x80070002
    WebApi
    多线程--程序员必修课
    委托(续2)
    委托(续)
    委托
  • 原文地址:https://www.cnblogs.com/tonix/p/4364581.html
Copyright © 2011-2022 走看看