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 ...
随机推荐
- cocos2d-x 的两大基类
cocos2d-x 有两个重要的基类,一个管理引用计数的 Ref,别一个则定义许多基本属性的 Node. 在 cocos2d-x 中的基本概念 说到 create 函数的时候提到 cocos2d-x ...
- Ubuntu下安装Java环境
1 Java 8 下载地址 http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk8-downloads-2133151-zhs. ...
- WCF开发指南之构建服务
一. 引言 Windows通讯基础(简称为WCF)是一种SDK,用于让你使用典型的CLR编程结构(例如用于发布和消费服务的类和接口等)来构建Windows面向服务的应用程序.WCF的编程模型是声明性的 ...
- init.sh 学习(转
cd /mnt insmod ss_triger_drv.ko insmod ss_led_alarm_drv.ko insmod ss_img_prc_drv.ko insmod ss_post_i ...
- linux命令:which
1.命令介绍: which用来在指定的路径搜索某个系统命令的位置,并返回第一个搜索结果. 2.命令格式: which [选项] 系统命令 3.命令参数: -n 指定文件名长度,指定的长度必须大于或等 ...
- Java-->打包发送信息(UDP协议)
--> 好像UDP 协议没有TCP 协议应用得那么广泛 --> UdpSender 类定义一个发送端(快递公司) package com.dragon.java.udpdatagram; ...
- [转载]MCU DSP ARM 嵌入式 之间的区别
2016-06-28 10:31:24) 嵌入式"这是个概念,准确的定义没有,各个书上都有各自的定义.但是主要思想是一样的,就是相比较PC机这种通用系统来说,嵌入式系统是个专用系统,结构精简 ...
- For xml path
Select * from tb for xml path('') 特点: 1. 以xml形式展示查询数据. 2. 自定义数据展示类型. 实例: 1. Select * from tb for xml ...
- Codeforces Round #157 (Div. 2)
A. Little Elephant and Chess 模拟. B. Little Elephant and Magic Square 枚举左上角,计算其余两个位置的值,在\(3\times 3\) ...
- Solidworks安装界面是英文的解决办法
[问题描述] 一样的安装文件,同事之前安装界面就是中文的,安装好以后软件也是中文的. 这几天系统挂了,重装系统后就发现安装界面是英文的,安装好以后也是英文. win7系统,位数不详,solidwork ...