KMP算法 C#实现 字符串查找简单实现
KMP算法 的C#实现,初级版本
static void Main(string[] args)
{
#region 随机字符
StringBuilder sb = new StringBuilder();
string S = "ABCDEFJHIJKLMNOPQRSTUVWXYZ0123456789";
Random r = new Random();
for (int f = ; f < ; f++)
{
if (f % == )
{
sb.Append("AABCDAABCD");
}
else
{
sb.Append(S.Substring(r.Next(, S.Length - ), ));
} }
string sourceStr = sb.ToString();
Console.WriteLine("待匹配的字符长度为:" + sourceStr.Length);
#endregion
Console.WriteLine("start");
Stopwatch sw = new Stopwatch();
sw.Start();
string ostr = "AABCDAABCD";//目标字符
/*
找到对应位置的最大移动长度
*/
Dictionary<int, int> dic = GetMaxMoveLength(ostr);
int success = ;
for (int i = ; i < sourceStr.Length; i++)
{
int j = ;
while (j < ostr.Length && i + j < sourceStr.Length && ostr[j] == sourceStr[i + j])
{
j++;
}
if (j == ostr.Length)
{
success++;
}
i += j;
}
sw.Stop();
Console.WriteLine("成功" + success+"个");
Console.WriteLine("时间:"+sw.ElapsedMilliseconds+"毫秒");
Console.WriteLine("End");
Console.ReadKey();
}
static Dictionary<int, int> GetMaxMoveLength(string ostr)
{
Dictionary<int, int> CanMoved = new Dictionary<int, int>();
for (int i = ; i < ostr.Length; i++)
{
List<string> q = new List<string>();
List<string> h = new List<string>();
string tempstr = ostr.Substring(, i + );
for (int j = ; j < tempstr.Length; j++)
{
q.Add(tempstr.Substring(, j));
}
for (int j = tempstr.Length - ; j > ; j--)
{
h.Add(tempstr.Substring(j, tempstr.Length - j));
}
//获取该位置的最大长度
IEnumerable<string> keys = q.Intersect(h);
int movelength = ;
foreach (string s in keys)
{
if (s.Length > movelength)
{
movelength = s.Length;
}
}
CanMoved.Add(i, i-movelength);
}
return CanMoved;
}

在42万5000个字符中匹配到25000个目标字符用时10毫秒
KMP算法 C#实现 字符串查找简单实现的更多相关文章
- KMP算法,匹配字符串模板(返回下标)
//KMP算法,匹配字符串模板 void getNext(int[] next, String t) { int n = next.length; for (int i = 1, j = 0; i & ...
- KMP算法与传统字符串寻找算法
原理:KMP算法是一种模板匹配算法,它首先对模板进行便利,对于模板中与模板首字符一样和首字符进行标志-1,对于模板匹配中出现不匹配的若是第一轮检查标志为0,若不是第一轮检查标志为该元素与标志为-1的距 ...
- 算法之暴力破解和kmp算法 判断A字符串是否包含B字符串
我们都知道java中有封装好的方法,用来比较A字符串是否包含B字符串 如下代码,contains,用法是 str1.contains(str2), 这个布尔型返回,存在返回true,不存在返回fals ...
- kmp算法,求重复字符串
public class Demo { public static void main(String[] args) { String s1 = "ADBCFHABESCACDABCDABC ...
- kmp算法原理与应用(简单易懂)
- KMP算法——字符匹配
暴力匹配: 假设现在我们面临这样一个问题:有一个文本串S,和一个模式串P,现在要查找P在S中的位置,怎么查找呢? 如果用暴力匹配的思路,并假设现在文本串S匹配到 i 位置,模式串P匹配到 j 位置, ...
- 回朔法/KMP算法-查找字符串
回朔法:在字符串查找的时候最容易想到的是暴力查找,也就是回朔法.其思路是将要寻找的串的每个字符取出,然后按顺序在源串中查找,如果找到则返回true,否则源串索引向后移动一位,再重复查找,直到找到返回t ...
- 字符串匹配算法之kmp算法
kmp算法是一种效率非常高的字符串匹配算法,是由Knuth,Morris,Pratt共同提出的模式匹配算法,所以简称KMP算法 算法思想 在一个字符串中查找另一个字符串时,会遇到如下图的情况 我们通常 ...
- 算法-最通俗易懂的KMP算法详解
有些算法,适合从它产生的动机,如何设计与解决问题这样正向地去介绍.但KMP算法真的不适合这样去学.最好的办法是先搞清楚它所用的数据结构是什么,再搞清楚怎么用,最后为什么的问题就会有恍然大悟的感觉.我试 ...
随机推荐
- Spring注解驱动开发之扩展原理
前言:现今SpringBoot.SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了Spring的一些底层注解.原理,比如@Conditional.@Import.@Ena ...
- DOM,javascript,Web API之间的关系——onclick 引起的思考与调研
平时习惯了用js操作dom树来与html页面进行交互,基本都是通过web API接口实现的,最近看闭包和原生js的知识点比较多,昨天无意中看到了onclick中的this指向问题,遂用native j ...
- 阿里云设置指定ip访问实例
添加安全组规则 添加允许访问的外网IP,优先级设置为1,并将所有ip设置为拒绝访问,优先级设置为2. 参考地址: https://help.aliyun.com/document_detail/254 ...
- asp IIS网站的配置(Win7下启用IIS7配置ASP运行环境)
其实win7下的IIS7配置过程是非常简单的.下面让seo博客来详细的介绍一下win7下配置IIS7环境运行ASP网站的方法,以供初接触者参考 第一次在windows7下配置IIS,虽然有丰富的x ...
- java SSL 邮件发送
Properties props = new Properties(); props.put("mail.smtp.host", smtp); props.put("ma ...
- Bash Template
#/bin/bash #set -x set -e usage() { cat <<EOF Usage: `basename $` [OPTIONS] <non-option arg ...
- Android APK生成证书并签名方法
Android APK生成证书并签名方法 android cordova keystore android证书签名 阅读:925 时间:2018年09月20日 Android开发者可能对此很熟悉.使用 ...
- 反射(hasattr,getattr,delattr,setattr)
反射(hasattr,getattr,setattr,delattr) 反射在类中的使用 反射就是通过字符串来操作类或者对象的属性 反射本质就是在使用内置函数,其中反射有四个内置函数: hasattr ...
- select 修改选中时候的默认默认样式 outline:none 把系统的线关了 然后自己再border一下
chrome 查看样式的时候默认没有 focus的样式,可以把选择器开开select 修改选中时候的默认默认样式 outline:none 把系统的线关了 然后自己再border一下input:foc ...
- Webstorm 的 Tab 键调整缩进值
两步即可,注意版本