LeetCode28:Implement strStr()
2015-12-01 10:39
495 查看
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
题目描述:实现strStr()函数,strStr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的位置;否则,返回-1。
方法一:暴力匹配
方法二:KMP字符串匹配算法
完整测试代码:
#include<iostream>
#include<string>
using namespace std;
//1.首先对模式串needle进行“自匹配”,求出模式串前缀与后缀的最大公共元素长度,
//并将长度保存到next数组中;
//2.根据next数组,对文本串haystack与模式串needle进行字符串匹配
class Solution{
public:
int strStr(string haystack, string needle){
int hlen = haystack.length();
int nlen = needle.length();
int* next = new int[nlen];
getNext(needle, next);
int i = 0;
int j = 0;
while (i < hlen&&j < nlen)
{
//如果j=-1,或者当前字符匹配成功(即haystack[i] == needle[j]),则继续匹配后面的字符
if (j == -1 || haystack[i] == needle[j]){
i++;
j++;
}
else{ //如果j不等于-1,且当前字符匹配失败,则i不变,j=next[j],模式串的needle[next[j]]字符与文本串haystack[i]进行匹配
j = next[j];
}
}
if (j == nlen) //模式串与文本串中某个子串完全匹配成功
return i - nlen;
else
return -1;
}
//对模式串needle进行自匹配,计算KMP算法的next数组
void getNext(string &needle1, int next[])
{
int nlen = needle1.size();
next[0] = -1;
int k = -1;
int j = 0;
while (j < nlen - 1)
{
//needle[k]表示前缀,needle[j]表示后缀
if (k == -1 || needle1[j] == needle1[k])
{
++k;
++j;
next[j] = k;
}
else
{
k = next[k];//通过递归前缀索引,找到长度更短的相同前缀后缀
}
}
}
};
int main()
{
string s = "ABC ABCDAB ABCDABCDABDE";
//string s = "ABCDABD";
string p = "ABCDABD";
Solution sol;
int n = sol.strStr(s, p);
cout << n << endl;
system("pause");
return 0;
}
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
题目描述:实现strStr()函数,strStr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的位置;否则,返回-1。
方法一:暴力匹配
class Solution { public: int strStr(string haystack, string needle) { int n=haystack.size(); int k=needle.size(); for(int i=0;i<=n-k;i++){ int j; for( j=0;j<k;j++){ if(haystack[i+j]!=needle[j]) break; } if(j==k) return i; } return -1; } };
方法二:KMP字符串匹配算法
//1.首先对模式串needle进行“自匹配”,求出模式串前缀与后缀的最大公共元素长度,并将长度保存到next数组中; //2.根据next数组,对文本串haystack与模式串needle进行字符串匹配 class Solution{ public: int strStr(string haystack, string needle){ int hlen = haystack.length(); int nlen = needle.length(); int* next = new int[nlen]; getNext(needle, next); int i = 0; int j = 0; while (i < hlen&&j < nlen) { //如果j=-1,或者当前字符匹配成功(即haystack[i] == needle[j]),则继续匹配后面的字符 if (j == -1 || haystack[i] == needle[j]){ i++; j++; } else{ //如果j不等于-1,且当前字符匹配失败,则i不变,j=next[j],模式串的needle[next[j]]字符与文本串haystack[i]进行匹配 j = next[j]; } } if (j == nlen) //模式串与文本串中某个子串完全匹配成功 return i - nlen; else return -1; } //对模式串needle进行自匹配,计算KMP算法的next数组 void getNext(string &needle1, int next[]) { int nlen = needle1.size(); next[0] = -1; int k = -1; int j = 0; while (j < nlen - 1) { //needle[k]表示前缀,needle[j]表示后缀 if (k == -1 || needle1[j] == needle1[k]) { ++k; ++j; next[j] = k; } else { k = next[k];//通过递归前缀索引,找到长度更短的相同前缀后缀 } } } };
完整测试代码:
#include<iostream>
#include<string>
using namespace std;
//1.首先对模式串needle进行“自匹配”,求出模式串前缀与后缀的最大公共元素长度,
//并将长度保存到next数组中;
//2.根据next数组,对文本串haystack与模式串needle进行字符串匹配
class Solution{
public:
int strStr(string haystack, string needle){
int hlen = haystack.length();
int nlen = needle.length();
int* next = new int[nlen];
getNext(needle, next);
int i = 0;
int j = 0;
while (i < hlen&&j < nlen)
{
//如果j=-1,或者当前字符匹配成功(即haystack[i] == needle[j]),则继续匹配后面的字符
if (j == -1 || haystack[i] == needle[j]){
i++;
j++;
}
else{ //如果j不等于-1,且当前字符匹配失败,则i不变,j=next[j],模式串的needle[next[j]]字符与文本串haystack[i]进行匹配
j = next[j];
}
}
if (j == nlen) //模式串与文本串中某个子串完全匹配成功
return i - nlen;
else
return -1;
}
//对模式串needle进行自匹配,计算KMP算法的next数组
void getNext(string &needle1, int next[])
{
int nlen = needle1.size();
next[0] = -1;
int k = -1;
int j = 0;
while (j < nlen - 1)
{
//needle[k]表示前缀,needle[j]表示后缀
if (k == -1 || needle1[j] == needle1[k])
{
++k;
++j;
next[j] = k;
}
else
{
k = next[k];//通过递归前缀索引,找到长度更短的相同前缀后缀
}
}
}
};
int main()
{
string s = "ABC ABCDAB ABCDABCDABDE";
//string s = "ABCDABD";
string p = "ABCDABD";
Solution sol;
int n = sol.strStr(s, p);
cout << n << endl;
system("pause");
return 0;
}
相关文章推荐
- spring源码剖析(四)自定义标签解析流程
- 解决OPPO手机adb调试找不到设备(Win8系统)
- Android Studio启动时卡在Fetching Android SDK 以及导入Eclipse工程慢的解决方法
- oracle net manager的配置文件tnsnames.ora位置
- Redis 64 steps
- http协议
- 时间格式化
- 每天一个linux命令(14):which命令
- jdbc获取insert主键
- Android-DialogFragment对话框之登录
- LVS+Keepalived实现高可用负载均衡
- crontab 定时任务
- 使用Glup.JS优化JS、CSS、图片,压缩合并
- 关于Activity的四种启动模式
- composer使用
- 每天一个linux命令(13):tail 命令
- Caffe C++API 提取任意一张图片的特征系列二----MemoryData
- 支付账户:appid\mch_id(微信支付商户号)\API密钥\Appsecret
- Java读书笔记十二(Java中的代码块)
- VC 如何使程序运行后自己删除自己