KMP算法的实现:

#include <stdio.h>
#include <string.h>
#include <stdlib.h> int strStr(char* haystack, char* needle) {
if (haystack == NULL || needle == NULL)
return -1;
if (needle[0] == '\0')
return 0;
int lenPattern = strlen(needle);
int lenHaystack = strlen(haystack);
int *shift = (int *)malloc(sizeof(int) * lenPattern);
int i;
shift[0] = 0; //for (i = 1; i < lenPattern; i++){
// if (needle[shift[i - 1]] == needle[i])
// shift[i] = shift[i - 1] + 1;
// else
// {
// if (needle[i] == needle[0])//这里有bug,但是leecode通过了,原因是刚好没碰到过不了的测试案例
// shift[i] = 1;
// else
// shift[i] = 0;
// }
//} //代码改写如下:
int j = 0;
shift[0] = j;
for (i = 1; i < lenPattern;) {
if (needle[i] == needle[j])
{
j++;
shift[i] = j;
i++;
}
else
{
if (j == 0)
{
shift[i] = 0;
i++;
}
else
{
j = shift[j - 1];
} }
} j = 0;
for (i = 0; i < lenHaystack;)
{
if (haystack[i] == needle[j])
{
i++;
j++;
if (j == lenPattern)
return (i - j);
}
else
{
if (j == 0)
{
i++;
}
else
{
j = shift[j - 1];
} }
}
return -1;
} int main()
{
//char *input = "";
//char *pattern = "";
char *input = "BBC ABCDAB ABCDABCDABDE";
char *pattern = "ABCDABD";
printf("%d\n", strStr(input, pattern)); return 0;
}

leecode之Implement strStr()的更多相关文章

  1. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  2. 28. Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. Leetcode 详解(Implement strstr)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  4. [leetcode 27]Implement strStr()

    1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  5. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  6. 【leetcode】Implement strStr() (easy)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  7. [LeetCode] Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  8. Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. Implement strStr() [LeetCode]

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

随机推荐

  1. P1049_装箱问题(JAVA语言)

    思路:动态规划的背包问题.使箱子剩余空间最小,也就是使箱内装的物品体积达到最大,我们可将物品的体积视为价值,然后按照01背包问题求解即可. //直接上模板 题目描述 有一个箱子容量为VV(正整数,0 ...

  2. 攻防世界 reverse Windows_Reverse2

    Windows_Reverse2   2019_DDCTF 查壳: 寻找oep-->dump-->iat修复   便可成功脱壳 int __cdecl main(int argc, con ...

  3. Android学习之探究Fragment

    •碎片是什么 Fragment是一种可以嵌入在活动中的UI片段,能够让程序更加合理和充分地利用大屏幕的空间: 出现的初衷是为了适应大屏幕的平板电脑,可以将其看成一个小型Activity,又称作Acti ...

  4. 极简实用的Asp.NetCore模块化框架决定免费开源了

    背景 在开发这个框架之前,前前后后看过好几款模块化的框架,最后在一段时间内对ABP VNext痛下狠心,研究一段时间后,不得不说 ABP VNext的代码层面很规范,也都是一些最佳实践,开发出一个模块 ...

  5. MacBook读写移动硬盘

    在MacBook上插入移动硬盘,只能读取,不能写入.这是因为移动硬盘的格式是NTFS,MacBook不支持写入,有三种方法: 1. 改变移动硬盘的格式,格式化为可以读写的exFAT等格式,但存储的文件 ...

  6. 面试关于Spring循环依赖问题,我建议你这么答!

    写在前面 在关于Spring的面试中,我们经常会被问到一个问题:Spring是如何解决循环依赖的问题的. 这个问题算是关于Spring的一个高频面试题,因为如果不刻意研读,相信即使读过源码,面试者也不 ...

  7. 201871010129-郑文潇 实验二 个人项目—《D{0-1}背包问题 》项目报告

    项目 内容 课程班级博客链接 课程链接 这个作业要求链接 [作业要求](https://www.cnblogs.com/nwnu-daizh/p/14552393.html) 我的课程学习目标 1.掌 ...

  8. Java(246-264)【List、Set】

    1.数据结构_栈 Stack先进后出 2.数据结构_队列 Queue先进先出 3.数据结构_数组 Array查询快.增删慢 需要创建一个新数组,将指定新元素存储在指定索引位置,再把原数组元素根 据索引 ...

  9. 铁人三项(第五赛区)_2018_seven

    铁人三项(第五赛区)_2018_seven 先来看看保护 保护全开,IDA分析 首先申请了mmap两个随机地址的空间,一个为rwx,一个为rw 读入的都shellcode长度小于等于7,且这7个字符不 ...

  10. Tony老师带你来看Java设计模式:代理模式

    目录 定义 作用 意图 主要解决问题 优缺点 与装饰者模式的区别 结构 从Tony老师来看实现方式 静态代理 动态代理 JDK动态代理的实现 cglib动态代理的实现 定义 为其他对象提供一种代理来控 ...