Implement strStr()

Implement strStr().

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

MY: Question.

思路: 逐步查找。当出现不同时,如何回溯是关键。

Solution A:

class Solution {
public:
char *strStr(char *haystack, char *needle) {
int i = 0, j = 0;
while(haystack[i] != '\0' && needle[j] != '\0') {
if(haystack[i] == needle[j])
++i, ++j;
else
i = i-j+1, j = 0;
}
return needle[j] == '\0' ? haystack+(i-j) : NULL;
}
};

Solution B 与经典 KMP 算法:

对模式串 P 设置回溯数组 next. (next 只有模式串 P 的特性有关系,与目标串没有关系。)

next 的求法:

next[0] = 0; (0 位置最后匹配,下次还从此位置开始匹配(舍去从-1开始,没有意义))

next[pos] = (P[next[pos-1]] == P[pos] ? next[pos-1]+1 : 0);

(若回溯后的字符与当前字符相同,则应设置回溯位置在前回溯位置之后。否则,设置回溯位置为0)

模式串中:

当前位置 pos 不能匹配时, 回溯到 next[pos-1] 重新开始匹配。

当前位置匹配,则继续下去。

#include <iostream>
#include <vector>
using namespace std; void getNext(char *P, vector<int> &next) {
for (int i = 0; P[i] != '\0'; ++i) {
if (i == 0) next.push_back(0);
else next.push_back((P[i] == P[next[i-1]]) ? next[i-1]+1 : 0);
}
}
class Solution {
public:
char *strStr(char *haystack, char *needle) {
vector<int> next;
getNext(needle, next);
int i = 0, j = 0;
while (haystack[i] != '\0' && needle[j] != '\0') {
if (haystack[i] == needle[j]) ++i, ++j;
else if (j == 0) ++i;
else j = next[j-1];
}
return needle[j] == '\0' ? haystack+(i-j) : NULL;
}
}; int main() {
char *T = "missiissippi", *P = "issip";
cout << (Solution().strStr(T, P) ? Solution().strStr(T, P) : "NULL") << endl;
return 0;
}

70. Implement strStr() 与 KMP算法的更多相关文章

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

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

  2. 第3章:LeetCode--算法:strStr KMP算法

    https://leetcode.com/problems/implement-strstr/  28. Implement strStr() 暴力算法: int ViolentMatch(char* ...

  3. 用KMP算法实现strStr()

    strStr()函数的用途是在一个字符串S中寻找某个字串P第一次出现的位置.并返回其下标,找不到时返回-1.最简单的办法就是找出S全部的子串和P进行比較,然而这种方法比較低效.假设我们从S的下标0和P ...

  4. Linux GCC下strstr的实现以及一个简单的Kmp算法的接口

    今天做了一道题,要用判断一个字符串是否是另一个字符串的子串,于是查了一下strstr的实现. 代码如下: char *strstr(const char*s1,const char*s2) { con ...

  5. 自己对kmp算法的理解,借由 28. 实现 strStr() 为例

    做题思路 or 感想 : 就借由这道题来理解一下kmp算法吧 kmp算法的操作过程我觉得有句话很合适 :KMP 算法永不回退 目标字符串 的指针 i,不走回头路(不会重复扫描 目标字符串),而是借助 ...

  6. 28. Implement strStr()(KMP字符串匹配算法)

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

  7. [leetcode 27]Implement strStr()

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

  8. Leetcode #28. Implement strStr()

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

  9. Implement strStr()

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

随机推荐

  1. Android通过HttpURLConnection链接到网络,并获取网络数据

    1.判断网络是否连接 private void networkIsconnected(String str){ ConnectivityManager connMgr = (ConnectivityM ...

  2. MYSQL中关于日期处理的函数

    < DOCTYPE HTML PUBLIC -WCDTD HTML TransitionalEN> MySQL数据库中SQL语句中 关于日期.时间\时间戳的函数   一 MySQL 获得当 ...

  3. AD 10 原理图编译错误

    在编译原理图时,经常会出现以下错误和警告,这里简单的累出一些错误和警告的原因: 1.floating net labels,这个是应为网标防止错误,应该将网标放在I/O或这连线的端点,或者是网标表错了 ...

  4. Java中文档制作与继承

    1:如何制作帮助文档(了解) (1)写一个类 (2)加入文档注释 (3)通过javadoc工具生成即可 javadoc -d 目录 -author -version ArrayTool.java 2: ...

  5. sqlserver数据库学习(-)数据类型

    ecimal 数据类型最多可存储 38 个数字,所有数字都能够放到小数点的右边.decimal 数据类型存储了一个准确(精确)的数字表达法:不存储值的近似值. 定义 decimal 的列.变量和参数的 ...

  6. css中的display以及position属性

    我们都知道,元素分为行内元素和块级元素,在页面布局中,我们常常需要让行内元素具有块级元素的特性,或者使块级元素转换成行内元素,这就要使用我们的display属性了. 我们先定义三个div: 以上的三个 ...

  7. hdu 2095

    ps:真是日了狗...英语渣渣理解题目不行,开了个100W数组来算,还优化了下时间,还是超时了,看了题解才知道用异或. N个数异或,会得出其中是奇数的一个.比如 1^1^3^2^2 = 3.   1^ ...

  8. RF Analyzer for Android 安卓平台连接HackRF的App

    Over the last week I've been working on a new project, trying to build a spectrum analyzer for Andro ...

  9. windows调试器尝鲜

    曾几何时,我也下载过看雪论坛精华看的津津有味.可惜一直没有动手去调试,学到的x86汇编指令也忘得差不多了.最近将老机器的T4200 CPU换成了更省电,温度更低的P8800,为了支援新的VT虚拟化,特 ...

  10. 计算DNA中每种核苷酸的数目

    问题描述:计算DNA中每种核苷酸的数目 输入文件内容: 代码: 输出结果: