KMP algorithm challenge string.Contains
KMP:
public int KMP (ReadOnlySpan<char> content, ReadOnlySpan<char> span) {
_next = new int[span.Length];
GetNext (span);
int i = 0;
int j = 0;
while (i < content.Length && j < span.Length) {
if (j == 0 || content[i] == span[j]) {
if(content[i]==span[j])
j++;
i++;
} else
j = _next[j];
}
if (j >= span.Length)
return i - span.Length;
else
return -1;
}
private void GetNext (ReadOnlySpan<char> content) {
_next[0] = 0;
for (int i = 1; i < _next.Length; i++) {
if (_next[i - 1] != 0) {
if (content[i] == content[_next[i - 1]])
_next[i] = _next[i - 1] + 1;
} else {
if (content[0] == content[i])
_next[i] = 1;
else
_next[i] = 0;
}
}
}
string.Contains and KMP benchmarks:
[Benchmark]
public void TestStringContain()
{
string s = "abcasdfasfdkjefasdfasdaaadfdfasdfasdfasdfjjsdjfjsglskdfjskdjfaskjdflkasjgksajdfksjdf";
bool x = s.Contains("asdfasd",StringComparison.Ordinal);
}
[Benchmark]
public void TestKMP()
{
int x = containKeyWords.KMP("abcasdfasfdkjefasdfasdaaadfdfasdfasdfasdfjjsdjfjsglskdfjskdjfaskjdflkasjgksajdfksjdf", "asdfasd");
}
result:

Complete defeat!
KMP algorithm challenge string.Contains的更多相关文章
- hdu, KMP algorithm, linear string search algorithm, a nice reference provided 分类: hdoj 2015-07-18 13:40 144人阅读 评论(0) 收藏
reference: Rabin-Karp and Knuth-Morris-Pratt Algorithms By TheLlama– TopCoder Member https://www.top ...
- hdu, KMP algorithm, linear string search algorithm, a nice reference provided
reference: Rabin-Karp and Knuth-Morris-Pratt Algorithms By TheLlama– TopCoder Member https://www.top ...
- The Weekly Web Dev Challenge: String Calculator
The Weekly Web Dev Challenge: String Calculator https://twitter.com/intent/tweet?text=I just complet ...
- KMP Algorithm 字符串匹配算法KMP小结
这篇小结主要是参考这篇帖子从头到尾彻底理解KMP,不得不佩服原作者,写的真是太详尽了,让博主产生了一种读学术论文的错觉.后来发现原作者是写书的,不由得更加敬佩了.博主不才,尝试着简化一些原帖子的内容, ...
- [Algorithm] Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- (KMP)Count the string -- hdu -- 3336
http://acm.hdu.edu.cn/showproblem.php?pid=3336 Count the string Time Limit: 2000/1000 MS (Java/Other ...
- Microsoft2013校园招聘笔试题及解答
继续求拍砖!!!! 1. You are managing the database of a book publichser, you currently store the book orders ...
- Microsoft2013校园招聘笔试题
Microsoft2013校园招聘笔试题 继续求拍砖!!!! 1. You are managing the database of a book publichser, you currently ...
- POJ 3336 Count the string (KMP+DP,好题)
参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...
随机推荐
- zigw 和 nanoWatch, libudev.so 和 XMR 挖矿程序查杀记录
最近这两天以来,服务器一致声音很响.本来以为有同事在运行大的程序,结果后来发现持续很长时间都是这样,并没有停的样子.后来查了一下,发现有几个可疑进程导致,干掉之后,果然服务器静悄悄了. 但是,问题并没 ...
- python接口自动化测试(五)-其它(认证&代理&超时配置)
有了前面几节的介绍,基本的接口测试是可以满足了.本节一些其它的高级技巧: 一.认证 1.基本认证: # -*- coding:utf-8 -*- import requests url = " ...
- Swoole PHP windows composer
直接下载了 Swoole PHP 的 Windows 版安装包来用,遇到需要 Composer. 先是下载了 composer.phar.在这里下的 https://getcomposer.org/d ...
- Python操作redis系列之 列表(list) (五)(转)
# -*- coding: utf-8 -*- import redis r =redis.Redis(host=") 1. Lpush 命令将一个或多个值插入到列表头部. 如果 key 不 ...
- [C#] VS2017中在某些目录下使用不了 .NET Core 2.0 问题的处理办法
作者: zyl910 一.缘由 最近遇到了一个奇怪的问题--明明已经在VS2017里装好了 .NET Core 2.0 SDK,且测试过新建一个 .NET Core 2.0控制台项目能成功使用.但是在 ...
- k8s cronjob设置作业失败后退出不重复执行
Optional parameters backoffLimit: Number of retries for pods launched by the job. If you want your p ...
- 8款基于Jquery的WEB前端动画特效
1.超炫酷的30个jQuery按钮悬停动画 按钮插件是最常见的jQuery插件之一,因为它用途广泛,而且配置起来最为方便.今天我们要分享的是30个超炫酷的jQuery悬停按钮动画,当我们将鼠标滑过按钮 ...
- CAS单点登陆,URL多出个参数jsessionid导致登陆失败问题
目录: 1.定位问题 2.问题产生的原因 3.解决问题 一 定位问题 首先,如下图所示:输入到地址栏的地址被302重定向到单点登录地址,地址由Response Headers中的参数Location所 ...
- java集合 线程安全
1.快速失败(fail-fast)和安全失败(fail-safe)? 一:快速失败(fail—fast) 在用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的内容进行了修改(增加.删除.修改),则 ...
- Linux系统时间同步方法小结
在Windwos中,系统时间的设置很简单,界面操作,通俗易懂,而且设置后,重启,关机都没关系.系统时间会自动保存在BIOS时钟里面,启动计算机的时候,系统会自动在BIOS里面取硬件时间,以保证时间的不 ...