Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

这个题考查的是KMP算法。先求特征向量,然后再进行匹配,确实能够大大提高效率。code例如以下:

class Solution {
public:
char *strStr(char *haystack, char *needle) {
if(strlen(haystack)==0&&strlen(needle)==0)
return haystack;
if(strlen(haystack)==0&&strlen(needle)!=0)
return NULL;
if(strlen(needle)==0)
return haystack;
int m=strlen(needle);
int *N = new int[m];
N[0]=0;
int i,j,k;
for(i=1;i<m;i++)
{
k=N[i-1];
while(k>0 && needle[i]!=needle[k])
{
k=N[k-1];
}
if(needle[i]==needle[k])
N[i]=k+1;
else
N[i]=0;
}
j=0;
for(i=0;i<strlen(haystack);i++)
{
while(j>0 && needle[j]!=haystack[i])
j=N[j-1];
if(needle[j]==haystack[i])
j++;
if(j==strlen(needle))
return haystack+i-j+1;
}
return NULL;
}
};

leetcode——Implement strStr() 实现字符串匹配函数(AC)的更多相关文章

  1. leetcode笔记 动态规划在字符串匹配中的应用

    目录 leetcode笔记 动态规划在字符串匹配中的应用 0 参考文献 1. [10. Regular Expression Matching] 1.1 题目 1.2 思路 && 解题 ...

  2. 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith

    [C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...

  3. C语言字符串匹配函数

    C语言字符串匹配函数,保存有需要时可以用: #include <stdio.h> #include <stdlib.h> #include <string.h> # ...

  4. python实现 字符串匹配函数

    通配符是 shell 命令中的重要功能,? 表示匹配任意 1 个字符,*表示匹配 0 个或多个字符.请使用你熟悉的编程语言实现一个字符串匹配函数,支持 ? 和 * 通配符.如 "a?cd*d ...

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

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

  6. LeetCode Implement strStr()(Sunday算法)

    LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...

  7. LeetCode: Implement strStr() [027]

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

  8. [LeetCode] Implement strStr()

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

  9. [Leetcode] implement strStr() (C++)

    Github leetcode 我的解题仓库   https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...

随机推荐

  1. The following signatures couldn't be verified because the public key is not available: NO_PUBKEY XXXX

    reference apt-key adv --recv-keys --keyserver keyserver.ubuntu.com XXXXXX apt-get update

  2. tensorflow-gpu install check

    https://gist.github.com/mrry/ee5dbcfdd045fa48a27d56664411d41c#file-tensorflow_self_check-py-L16

  3. oracle 存储过程,存储函数,包,

    http://heisetoufa.iteye.com/blog/366957 认识存储过程和函数 存储过程和函数也是一种PL/SQL块,是存入数据库的PL/SQL块.但存储过程和函数不同于已经介绍过 ...

  4. mysql时间的处理

    mysql中格式化时间为: 1,DATE_FORMAT(APPLYDATE,'%Y-%m-%d %H:%i:%S') AS APPLYDATE 2,DATE_FORMAT(CHKSIGNDATE, ' ...

  5. Spring框架针对dao层的jdbcTemplate操作crud之add添加数据库操作

    使用jdbcTemplate 原理是把加载驱动Class.forName("com.mysql.jdbc.Driver"); 和连接数据库Connection conn=Drive ...

  6. Nginx安装及基本配置

    本文内容: 90%来自以下网址:http://www.nginx.cn/install ,修改了一些版本信息 10%来自以下网址:http://nginx.org/en/docs/beginners_ ...

  7. 【HIHOCODER 1039】 字符消除

    链接 问题描述 小Hi最近在玩一个字符消除游戏.给定一个只包含大写字母"ABC"的字符串s,消除过程是如下进行的: 1)如果s包含长度超过1的由相同字母组成的子串,那么这些子串会被 ...

  8. RF新手常见问题总结--(基础篇)

    1. 经常有人问这个元素找不到,一般先排除这两个地方,再自己找找A:是否等待了足够的时间让元素加载 (增加sleep xx, wait Until xxx)B:  仔细查查,这个元素是否进入到另一个f ...

  9. NYOJ448寻找最大数,贪心~~

    寻找最大数 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 请在整数 n 中删除m个数字, 使得余下的数字按原次序组成的新数最大, 比如当n=92081346718538 ...

  10. [Istio]流量管理API v1alpha3路由规则

    Istio提供一个API进行流量管理,该API允许用户将请求路由到特定版本的服务,为弹性测试注入延迟和失败,添加断路器等,所有这些功能都不必更改应用程序本身的代码.Istio 1.0中引入新的流量管理 ...