70. Implement strStr() 与 KMP算法
Implement strStr()
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
思路: 逐步查找。当出现不同时,如何回溯是关键。
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算法的更多相关文章
- LeetCode Implement strStr()(Sunday算法)
LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...
- 第3章:LeetCode--算法:strStr KMP算法
https://leetcode.com/problems/implement-strstr/ 28. Implement strStr() 暴力算法: int ViolentMatch(char* ...
- 用KMP算法实现strStr()
strStr()函数的用途是在一个字符串S中寻找某个字串P第一次出现的位置.并返回其下标,找不到时返回-1.最简单的办法就是找出S全部的子串和P进行比較,然而这种方法比較低效.假设我们从S的下标0和P ...
- Linux GCC下strstr的实现以及一个简单的Kmp算法的接口
今天做了一道题,要用判断一个字符串是否是另一个字符串的子串,于是查了一下strstr的实现. 代码如下: char *strstr(const char*s1,const char*s2) { con ...
- 自己对kmp算法的理解,借由 28. 实现 strStr() 为例
做题思路 or 感想 : 就借由这道题来理解一下kmp算法吧 kmp算法的操作过程我觉得有句话很合适 :KMP 算法永不回退 目标字符串 的指针 i,不走回头路(不会重复扫描 目标字符串),而是借助 ...
- 28. Implement strStr()(KMP字符串匹配算法)
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- [leetcode 27]Implement strStr()
1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
随机推荐
- 1900. Brainwashing Device
http://acm.timus.ru/problem.aspx?space=1&num=1900 题目大意: 有N个车站,相邻车站之间形成一个段,这样就有N-1个段,每个段最多可以放一个洗脑 ...
- 【61测试】【dp】【二分】【前缀和】【树剖】
不要问我为什么昨天考的今天才贴解题报告.. 第一题: 给定3个字符串,求它们的最长公共子序列. 解: 考试时知道肯定是LCS的二维再加一维,用三维,可天堂有路你不走,地狱无门你偏来...灵机一动想出来 ...
- jQueryMobile控件之按钮
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 关于QGraphicsScene 和 QGraphicsView 和 QDialog 的杂乱笔记【或说指针复习。。】
LtCalibrateDlg::~LtCalibrateDlg() { if (m_pIplImageGray) cvReleaseImage(&m_pIplImageGray); MYDEL ...
- 《C++primer》v5 第3章 字符串、向量和数组 读书笔记 习题答案
本章问题 1.char *p="hello world";与char p[]="hello world"的问题. 简单说前者是一个指向字符串常量的指针,后者是一 ...
- good books
- Structs2中iterator的status属性的用法
iterator标签主要是用于迭代输出集合元素,如list set map 数组等,在使用<s:iterator/>标签的时候有三个属性值得我们关注 1. value属性:可选的属性,va ...
- Python 基礎 - while流程判斷
接續上次的代碼,是不是只有執行一次才就結束,想要再繼續猜,就要在執行一次,是不是有點挺麻煩的? 所以這次我們就來再多做一點點功能進去,讓代碼可以多次循環地執行代碼,Go.... 首先,我們先來了解一下 ...
- 关于 Unix 用户权限及进程权限及 Saved set-user-id
最近在看APUE,看到3.14节,fcntl的时候#include <fcntl.h>int fcntl(int fd, int cmd, .../* int arg */);出错返回-1 ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...