zoukankan      html  css  js  c++  java
  • KMP算法

    因为在做大文件上传的分析中需要用到一段字符串的匹配算法,所以重新学习了一次KMP算法.

            private int[] GetNextVal(string t)
            
    {
                
    int j = 0, k = -1;
                
    int[] nextVal =  new int[t.Length];

                nextVal[
    0= -1;

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

                        
    else
                        
    {
                            nextVal[j] 
    = nextVal[k];
                        }

                    }

                    
    else
                    
    {
                        k 
    = nextVal[k];
                    }

                }


                
    return nextVal;
            }


            private int KmpIndexOf(string s, string t)
            
    {
                
    int i = 0, j = 0, v;
                
    int[] nextVal = GetNextVal(t);

                
    while (i < s.Length && j < t.Length)
                
    {
                    
    if (j == -1 || s[i] == t[j])
                    
    {
                        i
    ++;
                        j
    ++;
                    }

                    
    else
                    
    {
                        j 
    = nextVal[j];
                    }

                }


                
    if (j >= t.Length)
                    v 
    = i - t.Length;
                
    else
                    v 
    = -1;

                
    return v;
            }
  • 相关阅读:
    使用rem来开发你的移动端网站
    在网页布局中合理使用inline formating context(IFC)
    构建OLAP CDP平台 Maven父子项目
    2014世界杯决赛观后感
    2013岁末总结
    11月11日上班杂谈
    这一年
    湖南联通发福利了C#为你月赚150M流量回家过年不再愁
    C# 实现对接电信交费易自动缴费 续(winio/winring0 自动填密码)
    C# 实现对接电信交费易自动缴费
  • 原文地址:https://www.cnblogs.com/afxcn/p/1231831.html
Copyright © 2011-2022 走看看