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;
            }
  • 相关阅读:
    vue2.0 移动端,下拉刷新,上拉加载更多 封装组件
    Mac 安装RN android开发环境
    JavaScript 复杂判断的更优雅写法
    JSBridge的原理及使用
    FlatList列表组件的使用
    React Native ScrollView中TextInput焦点问题
    pod update报错(Cocoapods: Failed to connect to GitHub to update the CocoaPods/Specs specs repo)报错解决方案
    如何在pc端通过adb连接手机调试,不用usb数据线
    react学习之搭建后台管理系统
    node+koa2+axios踩坑记
  • 原文地址:https://www.cnblogs.com/afxcn/p/1231831.html
Copyright © 2011-2022 走看看