KMP算法备忘

今天学习了一下串的模式匹配算法,在这里记一下,以后好参考.具体的原理就不说了,可以参考严蔚敏老师的<<数据结构>>一书第79页到第84页,那里讲得比较深.我用C#写了一下这个算法,并且实现为string类的扩展函数.代码如下:

 

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算法C#代码

                                                                                                             2009年2月8日

转载于:https://www.cnblogs.com/MichaelGuan/archive/2009/02/09/1386530.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值