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;
            }
  • 相关阅读:
    mysql常用语句、命令(增删改查功能)
    MySQL 入门知识点
    windows下安装mysql解压版
    第4章 HDFS操作
    第3章 Hadoop 2.x分布式集群搭建
    第2章 CentOS7集群环境配置
    第1章 VMware中安装CentOS7
    windows10激活方法
    linux系统下操作mysql数据库常见命令
    xampp搭建开源项目iwebshop后,服务器重启后再启动xampp显示组件都启动ok,但是实际启动失败解决办法
  • 原文地址:https://www.cnblogs.com/afxcn/p/1231831.html
Copyright © 2011-2022 走看看