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. CentOS安装Navicat

    首先,下载文件navicat120_mysql_cs_x64.tar.gz,然后用命令 tar -zxvf 解压.解压以后,进入解压目录,运行start_navicat,就可以运行了. 运行以后,可能 ...

  2. NOIP2011普及组 数字反转

    题目OJ链接: http://codevs.cn/problem/1130/ https://www.luogu.org/problemnew/show/P1307 2011年NOIP全国联赛普及组 ...

  3. 通过配置JVM参数解决生成日志存在乱码问题

    项目上生产环境时出现一个问题,生成的日志文件存在乱码,服务器编码为 LANG=zh_US.UTF-8,按理说中文不会存在乱码,这里存在一个问题,就是在SIT等其他环境我们使用的连接工具为Xshell, ...

  4. solr+zookeeper集群配置

    将solr配置文件交给zookeeper进行管理 ./zkcli.sh -zkhost node01:2181,node02:2181,node03:2181 -cmd upconfig -confd ...

  5. Unity应用架构设计(9)——构建统一的 Repository

    谈到 『Repository』 仓储模式,第一映像就是封装了对数据的访问和持久化.Repository 模式的理念核心是定义了一个规范,即接口『Interface』,在这个规范里面定义了访问以及持久化 ...

  6. 批量备份数据库脚本(PowerShell版)

    开始 昨天备份一个数据库拿来测试,发现备份后的文件非常大.后来去检查下使用的备份脚本,原来之前的备份脚本没有压缩功能. 现把之前的备份脚本修改下,支持压缩备份,和支持仅复制备份(CopyOnly). ...

  7. VNC Viewer 设置屏幕分辨率

    1.第一种方法:使用geometry参数进行调整 vncserver -geometry 1280x1024即可,之后通过window下vnc连接后的ubuntu分辨率即为1280x1024了,注意这 ...

  8. 爬虫破解js加密(一) 有道词典js加密参数 sign破解

    在爬虫过程中,经常给服务器造成压力(比如耗尽CPU,内存,带宽等),为了减少不必要的访问(比如爬虫),网页开发者就发明了反爬虫技术. 常见的反爬虫技术有封ip,user_agent,字体库,js加密, ...

  9. 利用git 进行多人协作开发

    现在,大部分项目都是用 git 来管理代码的,但当项目变大.多人协作时,git 的使用就变得复杂了,这时就需要在 git 使用的流程上来思考如何更优的使用 git. 对于大部分 web 项目而言,并不 ...

  10. Mac 终端Terminal光标移动快捷键

    声明: 转载自: http://blog.csdn.net/lgm252008/article/details/8253519 在Mac系统中并没有Home.End等键,所以在使用时并不是特别的顺手, ...