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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. The Weekly Web Dev Challenge: String Calculator

    The Weekly Web Dev Challenge: String Calculator https://twitter.com/intent/tweet?text=I just complet ...

  4. KMP Algorithm 字符串匹配算法KMP小结

    这篇小结主要是参考这篇帖子从头到尾彻底理解KMP,不得不佩服原作者,写的真是太详尽了,让博主产生了一种读学术论文的错觉.后来发现原作者是写书的,不由得更加敬佩了.博主不才,尝试着简化一些原帖子的内容, ...

  5. [Algorithm] Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  6. (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 ...

  7. Microsoft2013校园招聘笔试题及解答

    继续求拍砖!!!! 1. You are managing the database of a book publichser, you currently store the book orders ...

  8. Microsoft2013校园招聘笔试题

    Microsoft2013校园招聘笔试题 继续求拍砖!!!! 1. You are managing the database of a book publichser, you currently ...

  9. POJ 3336 Count the string (KMP+DP,好题)

    参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...

随机推荐

  1. curl 模拟表单post文件

    网上查询出来的几乎都是错误的,正确的应该是: $data = array( 'pic'=>new CURLFile($path) // 如果无效可以这样 // 'pic'=>curl_fi ...

  2. 免费ss账号网站

    下面网址按排序顺序优先使用,数字越小优先级越高 1,https://io.freess.today/ 2,https://free-ss.site/ 3,https://ss.freess.org/ ...

  3. Open Graph Protocol(开放内容协议)

    最近在整理公司hexo博客的时候突然发现在页面 head 里面有一个这个奇怪的 meta Open Graph Protocol(开放内容协议) 开放内容协议一种新的HTTP头部标记,即这种协议可以让 ...

  4. 突破拐点:企业成长的S曲线

    1.企业成长的不同时期 初创期:初创前几年,最重要的创业激情,靠“蓝色小药丸”,断层期切入,不断试错玩命干(产品定位.商业模式.融资源.找关系.辅渠道),并最终完成赢利模式,得以生存. 成长期:通过快 ...

  5. Linux Shell 运算符

    Shell 和其他编程语言一样,支持多种运算符,包括: 算数运算符 关系运算符 布尔运算符 逻辑运算符 字符串运算符 文件测试运算符 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 ...

  6. nginx伪静态配置教程总结

    在nginx中配置伪静态,也就是常说的url重写功能,只需在nginx.conf配置文件中写入重写规则即可. 当然,这个规则是需要熟悉正则表达式,只掌握nginx自身的正则匹配模式即可,对正则不了解的 ...

  7. xz -d Python-3.4.7.tar.xz

    xz -d Python-3.4.7.tar.xz------>Python-3.4.7.tar tar -xvf Python-3.4.7.tar

  8. 5款最好的免费在线网站CSS验证器

    这里是一个名单, 5免费在线CSS验证器的网站.这些网站让你验证你的CSS代码的自由,没有任何麻烦.你可以选择上传文件,验证CSS添加URL,或简单的复制和粘贴完整的CSS代码.好的方面是,这些网站不 ...

  9. Gluon Datasets and DataLoader

    mxnet.recordio MXRecordIO Reads/writes RecordIO data format, supporting sequential read and write. r ...

  10. [转]list的交集,差集,并集

    原文地址:https://www.cnblogs.com/changfanchangle/p/8966860.html 工作中用到了list的取差集,发现还是挺好用的.所以记录下. 需求 list的方 ...