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#实现 字符串查找简单实现的更多相关文章

  1. KMP算法,匹配字符串模板(返回下标)

    //KMP算法,匹配字符串模板 void getNext(int[] next, String t) { int n = next.length; for (int i = 1, j = 0; i & ...

  2. KMP算法与传统字符串寻找算法

    原理:KMP算法是一种模板匹配算法,它首先对模板进行便利,对于模板中与模板首字符一样和首字符进行标志-1,对于模板匹配中出现不匹配的若是第一轮检查标志为0,若不是第一轮检查标志为该元素与标志为-1的距 ...

  3. 算法之暴力破解和kmp算法 判断A字符串是否包含B字符串

    我们都知道java中有封装好的方法,用来比较A字符串是否包含B字符串 如下代码,contains,用法是 str1.contains(str2), 这个布尔型返回,存在返回true,不存在返回fals ...

  4. kmp算法,求重复字符串

    public class Demo { public static void main(String[] args) { String s1 = "ADBCFHABESCACDABCDABC ...

  5. kmp算法原理与应用(简单易懂)

  6. KMP算法——字符匹配

     暴力匹配: 假设现在我们面临这样一个问题:有一个文本串S,和一个模式串P,现在要查找P在S中的位置,怎么查找呢? 如果用暴力匹配的思路,并假设现在文本串S匹配到 i 位置,模式串P匹配到 j 位置, ...

  7. 回朔法/KMP算法-查找字符串

    回朔法:在字符串查找的时候最容易想到的是暴力查找,也就是回朔法.其思路是将要寻找的串的每个字符取出,然后按顺序在源串中查找,如果找到则返回true,否则源串索引向后移动一位,再重复查找,直到找到返回t ...

  8. 字符串匹配算法之kmp算法

    kmp算法是一种效率非常高的字符串匹配算法,是由Knuth,Morris,Pratt共同提出的模式匹配算法,所以简称KMP算法 算法思想 在一个字符串中查找另一个字符串时,会遇到如下图的情况 我们通常 ...

  9. 算法-最通俗易懂的KMP算法详解

    有些算法,适合从它产生的动机,如何设计与解决问题这样正向地去介绍.但KMP算法真的不适合这样去学.最好的办法是先搞清楚它所用的数据结构是什么,再搞清楚怎么用,最后为什么的问题就会有恍然大悟的感觉.我试 ...

随机推荐

  1. 提交应用 Windows Phone的应用程序认证要求

    本文介绍了 Windows Phone 应用程序或游戏要通过认证并在 Windows Phone Marketplace 中发布而必须满足的策略和技术要求. 1.0 计划概述 设计认证过程的一个核心原 ...

  2. iOS Programming State Restoration 状态存储

    iOS Programming State Restoration 状态存储 If iOS ever needs more memory and your application is in the ...

  3. Node.js——环境变量

  4. <在此处打开命令窗口>替换为PowerShell打开模式

    Windows7中Shift+右键"在此处打开命令窗口"默认是采用cmd的方式打开. 把cmd替换为PowerShell的方式打开. 1. Ctrl + R 输入regedit进入 ...

  5. iTOP-4412开发板-实战教程-ssh服务器移植到arm开发板

    本文转自迅为开发板:http://www.topeetboard.com 在前面实战教程中,移植了“串口文件传输工具”,整个移植过程是比较简单的,而且我 们没有做任何协议方面的了解,只是“配置”+“编 ...

  6. Servlet相关的几种中文乱码问题

    Servlet相关的几种中文乱码问题浏览器调用jsp,html等页面中文显示乱码使得文件本身以utf-8字符集编辑保存 让浏览器浏览器以utf-8字符集解析 在浏览器中右键选择编码格式为utf-8: ...

  7. CSS 文字换行与不换行

    1. 强制不换行 p{ white-space:nowrap; } 2. 自动换行 p{ word-wrap: break-word; word-break: normal; } 3. 强制英文单词断 ...

  8. 数组,寻找第K大的数

    时间复杂度 O(n) def partition(data,left,right): if (len(data)<=0 or left<0 or right>=len(data)): ...

  9. CREATE AGGREGATE - 定义一个新的聚集函数

    SYNOPSIS CREATE AGGREGATE name ( BASETYPE = input_data_type, SFUNC = sfunc, STYPE = state_data_type ...

  10. 06CSS列表

    CSS列表 列表样式——list-style-type list-style-type:<属性值> disc   黑圆点 circle 空心圆点 square   小黑方块 decimal ...