zoukankan      html  css  js  c++  java
  • 字符串匹配算法之RK算法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace PK
    {
        class Program
        {
            static void Main(string[] args)
            {
    
    
                Console.WriteLine(RK("abcdef", "abce"));
                Console.WriteLine(RK("abcdef", "ab"));
                Console.WriteLine(RK("abcdef", "bc"));
                Console.WriteLine(RK("abcdef", "cd"));
                Console.WriteLine(RK("abcdef", "de"));
                Console.WriteLine(RK("abcdef", "ef"));
                Console.WriteLine(RK("abcdef", "ef3"));
                Console.ReadKey();
            }
    
    
    
            private static int RK(string resStr, string pStr)
            {
                int result = -1;
                int pStrHash = pStr.GetHashCode();
    
                int resStrLength = resStr.Length;
                int pStrLength = pStr.Length;
    
    
                for (int i = 0; i < resStrLength; i++)
                {
                    int stepEnd = i + pStrLength;
                    int stepStart = i;
                    if (stepEnd > resStrLength) return result;
                    string tmpStr = "";
                    for (int j = stepStart; j < stepEnd; j++)
                    {
                        tmpStr += resStr[j];
                        if (tmpStr.GetHashCode() == pStrHash)
                        {
                            result = stepStart;
                            return result;
                        }
                    }
                }
                return result;
            }
        }
    }

    打印结果:

  • 相关阅读:
    Jenkins+ant循环执行jmeter文件
    Jmeter接口模版说明
    jenkins与远程服务器配置SSH免密登录
    xcode developer tools简介
    MySQL之模糊查询
    MySQL排名函数
    openblas下载安装与使用
    CVX安装使用
    AMD包下载及使用
    Python及相应软件安装
  • 原文地址:https://www.cnblogs.com/weiqiangwaideshijie/p/14001043.html
Copyright © 2011-2022 走看看