题目描述:

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

分析:

这个题目考察的是KMP算法,这个算法的核心是求出子串的自匹配数组,在失配情况下不回溯父串指针(其实是索引),只需将子串向左滑动即可,next数组就是记录了滑动的距离的数组,求next数组是这个算法的难点。

我的第一个版本的代码如下:

#include <iostream>
#include <string>
#include <vector> using namespace std; int strStr(string haystack, string needle);
void getNext(vector<int> &ivec, const string & s); int main()
{
cout << "Hello world!" << endl;
string hay("mississippi");
string needle("issi");
int res = strStr(hay, needle);
cout << "the result of matching:" << res << endl;
system("pause");
return ;
} void getNext(vector<int> &ivec, const string & s)
{
int i = , j = -;
ivec[i] = j;
while (i < s.size() - )
{
if (j == - || s[i] == s[j])
ivec[++i] = ++j;
else
j = ivec[j];
}
} int strStr(string haystack, string needle)
{
if (needle.size() == )
return ;
if (haystack.size() == )
return -; vector<int> ivec(needle.size(), );
getNext(ivec, needle); for (auto i = ivec.begin(); i != ivec.end(); i++)
cout << *i << endl; int i = , j = ;
cout << haystack.size() << " " << needle.size() << endl; while( i < haystack.size() && j < needle.size() )
//while (i != haystack.size() && j != needle.size())
{
cout << "before modified " << i << " " << j << endl;
if (j == - || haystack[i] == needle[j])
{
cout << "what" << endl;
++i;
++j;
}
else
{
j = ivec[j];
}
cout << "after modified " << i << " " << j << endl; }
if (j == needle.size())
return i - j;
else
return -; }

这份代码我以为可以完美AC了,结果报了wrong answer,我很不理解,经过修改,就是将上一个版本中的while循环头换成下方紧邻的那一行就AC了,经过和大神的讨论,大神很快指出了我的错误所在,那就是C++的隐式类型转换!haystack.size()返回的是一个unsigned int类型,而i, j是int型,在运算时int会隐式转换为unsigned int,而-1转换为unsigned int型之后为4294967295(2的32次方减1)是一个非常大的数字!以前写循环不注意,现在吃了大亏!希望吃一堑长一智吧,以后写代码一定要非常注意类型的选择,注意int和unsigned int不能比较,如果必须比较一定将unsigned int 强制转换为long long类型再比较!最后完成版的代码如下:

#include <iostream>
#include <string>
#include <vector> using namespace std; int strStr(string haystack, string needle);
void getNext(vector<int> &ivec, const string & s); int main()
{
string hay("mississippi");
string needle("issi");
int res = strStr(hay, needle);
cout << "the result of matching:" << res << endl;
system("pause");
return ;
} void getNext(vector<int> &ivec, const string & s)
{
int i = , j = -;
ivec[i] = j;
while (i < s.size() - )
{
if (j == - || s[i] == s[j])
ivec[++i] = ++j;
else
j = ivec[j];
}
} int strStr(string haystack, string needle)
{
if (needle.size() == )
return ;
if (haystack.size() == )
return -; vector<int> ivec(needle.size(), );
getNext(ivec, needle); for (auto i = ivec.begin(); i != ivec.end(); i++)
cout << *i << endl; int i = , j = ;
long long haystack_size = haystack.size();
long long needle_size = needle.size(); while( i < haystack_size && j < needle_size )
{
cout << "i and j before modified " << i << " " << j << endl;
if (j == - || haystack[i] == needle[j])
{
cout << "what" << endl;
++i;
++j;
}
else
{
j = ivec[j];
}
cout << "i and j after modified " << i << " " << j << endl;
haystack_size = haystack.size();
needle_size = needle.size(); }
if (j == needle.size())
return i - j;
else
return -; }

leetcode 28的更多相关文章

  1. 前端与算法 leetcode 28.实现 strStr()

    # 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和 ...

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

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

  3. Java实现 LeetCode 28 实现strStr()

    28. 实现 strStr() 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ...

  4. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  5. LeetCode 28 Implement strStr() (实现找子串函数)

    题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description   Problem : 实现找子串的操作:如果没有找到则返回 ...

  6. LeetCode(28)题解:Implement strStr()

    https://leetcode.com/problems/implement-strstr/ 题目: Implement strStr(). Returns the index of the fir ...

  7. <每日 1 OJ> -LeetCode 28. 实现 strStr()

    题目: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存 ...

  8. Leetcode #28. Implement strStr()

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

  9. Java [leetcode 28]Implement strStr()

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

随机推荐

  1. 修改nignx报错Nginx [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)

    Nginx [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use) 这个错误是修改了nginx的配置时出现,表名80端口被程 ...

  2. NOIP2009潜伏者【B003】

    [B003]潜伏者[难度B]—————————————————————————————————————————————————————————————————— [题目要求] R国和S国正陷入战火之中 ...

  3. AngularJs表单验证

    常用的表单验证指令 1. 必填项验证 某个表单输入是否已填写,只要在输入字段元素上添加HTML5标记required即可: <input type="text" requir ...

  4. CodeForces 518B. Tanya and Postcard

    B. Tanya and Postcard time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  5. Unity Lightmap动态加载研究

    什么情况下需要Lightmap? 移动平台上目前暂时还不能开实时光影效果,会卡成幻灯片.所以就需要将光影烘焙到贴图上. 什么情况下需要动态加载Lightmap? 1.当项目抛弃了Unity的多场景模式 ...

  6. Memcache及telnent命令详解

    1.启动Memcache 常用参数 memcached 1.4.3 -p <num>      设置端口号(默认不设置为: 11211) -U <num>      UDP监听 ...

  7. 【PC网站前端架构探讨系列】关于中小型PC网站前端架构方案的讨论与实践

    目   录 1.遇到的问题 2.目标 3.探讨 4.架构设想 5.流程 6.初步实现 7.存在问题 8.最后 遇到的问题 我在这个系列上篇文章 已经讲解并开始逐步应用模块化思想,不知大家还记不记得,题 ...

  8. Run P4 without P4factory - A Simple Example In Tutorials.

    前言 本文是我运行P4社区于Github开源教程Tutorials中的P4 SIGCOMM 2015 Tutorial一些实战小结,Github链接: Github. 测试的例子:P4 SIGCOMM ...

  9. js获取cookie 和 模仿php的&_GET方法

    //获取get参数 function _get(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)( ...

  10. Mvc自定义路由让支持.html的格式

    前言 在大多时候,我们都需要自定义路由,当我们设置为/list/1.html的时候,有的时候会出现以下异常. routes.MapRoute( "category", // 路由名 ...