zoukankan      html  css  js  c++  java
  • KMP模式匹配算法程序(Python,C++,C)

    代码来自维基教科书:Knuth-Morris-Pratt pattern matcher

    Python程序如下:

    # Knuth-Morris-Pratt string matching
    # David Eppstein, UC Irvine, 1 Mar 2002
    
    #from http://code.activestate.com/recipes/117214/
    def KnuthMorrisPratt(text, pattern):
    
        '''Yields all starting positions of copies of the pattern in the text.
    Calling conventions are similar to string.find, but its arguments can be
    lists or iterators, not just strings, it returns all matches, not just
    the first one, and it does not need the whole text in memory at once.
    Whenever it yields, it will have read the text exactly up to and including
    the match that caused the yield.'''
    
        # allow indexing into pattern and protect against change during yield
        pattern = list(pattern)
    
        # build table of shift amounts
        shifts = [1] * (len(pattern) + 1)
        shift = 1
        for pos in range(len(pattern)):
            while shift <= pos and pattern[pos] != pattern[pos-shift]:
                shift += shifts[pos-shift]
            shifts[pos+1] = shift
    
        # do the actual search
        startPos = 0
        matchLen = 0
        for c in text:
            while matchLen == len(pattern) or 
                  matchLen >= 0 and pattern[matchLen] != c:
                startPos += shifts[matchLen]
                matchLen -= shifts[matchLen]
            matchLen += 1
            if matchLen == len(pattern):
                yield startPos

    C++程序如下:

    #include <iostream>
    #include <vector>
    using namespace std;
    
    //----------------------------
    //Returns a vector containing the zero based index of 
    //  the start of each match of the string K in S.
    //  Matches may overlap
    //----------------------------
    vector<int> KMP(string S, string K)
    {
            vector<int> T(K.size() + 1, -1);
    	vector<int> matches;
    
            if(K.size() == 0)
            {
                matches.push_back(0);
                return matches;
            }
    	for(int i = 1; i <= K.size(); i++)
    	{
    		int pos = T[i - 1];
    		while(pos != -1 && K[pos] != K[i - 1]) pos = T[pos];
    		T[i] = pos + 1;
    	}
    
    	int sp = 0;
    	int kp = 0;
    	while(sp < S.size())
    	{
    		while(kp != -1 && (kp == K.size() || K[kp] != S[sp])) kp = T[kp];
    		kp++;
    		sp++;
    		if(kp == K.size()) matches.push_back(sp - K.size());
    	}
    	
    	return matches;
    }

    C程序如下:

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
     
    void computeLPSArray(char *pat, int M, int *lps);
     
    void KMPSearch(char *pat, char *txt)
    {
        int M = strlen(pat);
        int N = strlen(txt);
     
        // create lps[] that will hold the longest prefix suffix values for pattern
        int *lps = (int *)malloc(sizeof(int)*M);
        int j  = 0;  // index for pat[]
     
        // Preprocess the pattern (calculate lps[] array)
        computeLPSArray(pat, M, lps);
     
        int i = 0;  // index for txt[]
        while(i < N)
        {
          if(pat[j] == txt[i])
          {
            j++;
            i++;
          }
     
          if (j == M)
          {
            printf("Found pattern at index %d 
    ", i-j);
            j = lps[j-1];
          }
     
          // mismatch after j matches
          else if(pat[j] != txt[i])
          {
            // Do not match lps[0..lps[j-1]] characters,
            // they will match anyway
            if(j != 0)
             j = lps[j-1];
            else
             i = i+1;
          }
        }
        free(lps); // to avoid memory leak
    }
     
    void computeLPSArray(char *pat, int M, int *lps)
    {
        int len = 0;  // lenght of the previous longest prefix suffix
        int i;
     
        lps[0] = 0; // lps[0] is always 0
        i = 1;
     
        // the loop calculates lps[i] for i = 1 to M-1
        while(i < M)
        {
           if(pat[i] == pat[len])
           {
             len++;
             lps[i] = len;
             i++;
           }
           else // (pat[i] != pat[len])
           {
             if( len != 0 )
             {
               // This is tricky. Consider the example AAACAAAA and i = 7.
               len = lps[len-1];
     
               // Also, note that we do not increment i here
             }
             else // if (len == 0)
             {
               lps[i] = 0;
               i++;
             }
           }
        }
    }
     
    // Driver program to test above function
    int main()
    {
       char *txt = "apurba mandal loves ayoshi loves";
       char *pat = "loves";
       KMPSearch(pat, txt);
       return 0;
    }


  • 相关阅读:
    基于Form组件实现的增删改和基于ModelForm实现的增删改
    Git和Github的基本操作
    如果获取的数据不是直接可以展示的结构---三种操作方式
    可迭代对象和迭代器生成器
    django之整体复习
    权限管理之大致流程
    kindedit编辑器和xxs攻击防护(BeautifulSoup)的简单使用
    博客系统之评论树与评论楼相关操作
    中介模型以及优化查询以及CBV模式
    angularjs中ajax请求时传递参数的方法
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7564848.html
Copyright © 2011-2022 走看看