KMP algorithm memo

Today, learning a bit string pattern matching algorithm, here remember what, after a good reference. Specifically principle is not to say, Yan Wei Min teacher can refer to the data structure << >> a book pages 79 to 84, where talk I wrote a bit deeper than C # using this algorithm, and implemented as an extension function string class code is as follows:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
    using System;

    
class KMPClass
ExpandedBlockStart.gifContractedBlock.gif    
{
        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string mother = "concatenation";
            
string child = "cat";
            
int k = mother.indexKMP(child);
            Console.WriteLine(k);
            Console.Read();
        }

    }


    
public static class MyClass
ExpandedBlockStart.gifContractedBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 简单版本,效率低
        
/// </summary>
        
/// <param name="str"></param>
        
/// <param name="child"></param>
        
/// <returns></returns>

        public static int index(this string str,string child)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
int i = 0;
            
int j = 0;
            
while (i < str.Length  && j < child.Length )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (str[i] == child[j])
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    i
++;
                    j
++;
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    i 
= i - j + 1;
                    j 
= 0;
                }


               
            }

            
if (j >child.Length - 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return i - child.Length;
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return -1;
            }


        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// KMP算法
        
/// </summary>
        
/// <param name="str"></param>
        
/// <param name="pattern"></param>
        
/// <returns></returns>

        public static int indexKMP(this string str, string pattern)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
int i = 0;
            
int j = 0;
            
int[] nextVal = GetNextVal(pattern);

            
while (i<str.Length && j<pattern.Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (j == -1 || str[i] == pattern[j])
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    i
++;
                    j
++;
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    j 
= nextVal[j];
                }

            }


            
if (j > pattern.Length - 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return i - pattern.Length;
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return -1;
            }

        }


        
private static int[] GetNextVal(string pattern)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
int j = 0, k = -1;
            
int[] nextVal = new int[pattern.Length];

            nextVal[
0= -1;

            
while (j < pattern.Length - 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (k == -1 || pattern[j] == pattern[k])
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    j
++;
                    k
++;
                    
if (pattern[j] != pattern[k])
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        nextVal[j] 
= k;
                    }

                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        nextVal[j] 
= nextVal[k];
                    }

                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    k 
= nextVal[k];
                }

            }


            
return nextVal;
        }

    }


KMP algorithm C # code

                                                                                                             February 8, 2009

Reproduced in: https: //www.cnblogs.com/MichaelGuan/archive/2009/02/09/1386530.html

Guess you like

Origin blog.csdn.net/weixin_33985507/article/details/93292386