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. ionic ion-tab图标修改, 自定义tab图标

    遇到需要自定义tab图标很简单, 只需要自定义相应的css就可以了, 这里要注意的是如何调整背景图片的大小. <ion-view hide-back-button="false&quo ...

  2. 【html+css3】在一张jpg图片上,显示多张透明的png图片

    1.需求:在一个div布局里面放置整张jpg图片,然后在jpg图片上显示三张水平展示的透明png图片,且png外层用a标签包含菜单 2.效果图: 3.上图,底层使用蓝色jpg图片,[首页].[购物车] ...

  3. 2、金融之关于BOLL

    一.BOLL(1)什么是BOLL线☆ BOLL指标又叫布林线指标(Bolinger Bands),是由约翰·布林格(John Bollinger)根据统计学中的标准差原理设计出来的一种非常简单实用的技 ...

  4. easyui的datagrid和treegrid的使用

    $('#listTree').treegrid({ idField: 'id', treeField: 'menuName', columns: [[ { title: 'Task Name', fi ...

  5. Linux 安装Zookeeper集群

    1.解压,配置环境变量 export ZOOKEEPER_HOME=/usr/local/zkexport PATH=.:$HADOOP_HOME/bin:$ZOOKEEPER_HOME/bin:$J ...

  6. 微信小程序——购物车结算

    项目需要做个购物车结算功能,先分析需求: 1.全选,反选的功能.当选中的个数 = 购物车的数量时,勾选全选按钮,反之则取消选中全选按钮: 2.改变选中状态时,计算总价和总数量: 3.单个产品的数量加减 ...

  7. go对json对象的生成和解析

    https://blog.csdn.net/benben_2015/article/details/78917374

  8. 【2019年04月22日】A股最便宜的股票

      太钢不锈(SZ000825) - 当前便宜指数:170.67 - 滚动扣非市盈率PE:4.37 - 滚动市净率PB:0.98 - 动态年化股息收益率:4.79%- 太钢不锈(SZ000825)的历 ...

  9. 【多媒体封装格式详解】--- AAC ADTS格式分析

    ADTS全称是(Audio Data Transport Stream),是AAC的一种十分常见的传输格式. 记得第一次做demux的时候,把AAC音频的ES流从FLV封装格式中抽出来送给硬件解码器时 ...

  10. Tomcat启动报错:This file is needed to run this program

    Tomcat版本为 8.5.29. 1.情景 kill调用tomcat进程后,重启tomcat报如下错误: Cannot find /usr/local/apache-tomcat-/bin/setc ...