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. CF524F And Yet Another Bracket Sequence 题解

    题目链接 算法:后缀数组+ST表+贪心   各路题解都没怎么看懂,只会常数巨大的后缀数组+ST表,最大点用时 \(4s\), 刚好可以过... 确定合法序列长度   首先一个括号序列是合法的必须满足以 ...

  2. PTA 找出不是两个数组共有的元素

    7-2 找出不是两个数组共有的元素 (20 分)   给定两个整型数组,本题要求找出不是两者共有的元素. 输入格式: 输入分别在两行中给出两个整型数组,每行先给出正整数N(≤),随后是N个整数,其间以 ...

  3. javaIO中的序列化和反序列化

    javaIO中的序列化和反序列化 1.什么是序列化?它是来解决什么问题的 1.我们创建的对象,一般情况下在内存中,程序关闭,或者因为没有地址指向而导致垃圾回收 2.这样,我们的对象就会丢失 3.那么我 ...

  4. Android学习之异步消息处理机制

    •前言 我们在开发 APP 的过程中,经常需要更新 UI: 但是 Android 的 UI 线程是不安全的: 如果想更新 UI 线程,必须在进程的主线程中: 这里我们引用了异步消息处理机制来解决之一问 ...

  5. abp加DDD开发:低耦合、可复用、可扩展的【工单】业务模块-简介和集成

    前言 很多场景[单体+模块化]比微服务更合适,开发难度低.代码可复用性强.可扩展性强.模块化开发有些难点,模块启动与卸载.模块之间的依赖和通讯.asp.net core abp为我们提供了模块化开发能 ...

  6. HashMap源码个人解读

    HashMap的源码比较复杂,最近也是结合视频以及其余大佬的博客,想着记录一下自己的理解或者当作笔记 JDK1.8后,HashMap底层是数组+链表+红黑树.在这之前都是数组+链表,而改变的原因也就是 ...

  7. KeyError:‘uid' Python常见错误

    使用不存在的字典键值 检查字典和要查的内容 如有不正确改正即可

  8. kubernetes:基于ab的压力测试

    基于ab的压力测试 # cat apache-test.yaml ################################################################### ...

  9. LibTorch实战六:C++版本YOLOV5.4的部署

    一.环境配置 win10 vs2017 libtorch-win-shared-with-deps-debug-1.8.1+cpu opencv349 由于yolov5代码,作者还在更新(写这篇博客的 ...

  10. 如果你的application.properties中还存在明文密码----加密Spring Boot中的application.properties

    1 概述 什么?都2020年了还在Spring Boot的配置文件中写明文密码? 虽然是小项目,明文也没人看. 明文简单快捷方便啊!!! 你看直接用户名root密码123456多么简单!!! ... ...