题目描述:

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. iOS--创建炫酷的渐变色界面

    { CAGradientLayer *_layer; } //创建渐变层 _layer =[CAGradientLayer layer]; _layer.frame=self.view.frame; ...

  2. 练练脑javascript写直接插入排序和冒泡排序

    function insertionSort(array) { if (Object.prototype.toString.call(array).slice(8, -1) === 'Array') ...

  3. jvm的垃圾回收原理

    什么是垃圾回收? 垃圾回收是Java中自动内存管理的另一种叫法.垃圾回收的目的是为程序保持尽可能多的可用堆(heap). JVM会删除堆上不再需要从堆引用的对象. 用一个例子解释垃圾回收? 比方说,下 ...

  4. a 标签 启用或禁用点击事件

    <a href="#" id="btnAuthCode" class="authCode_btn">获取验证码</a> ...

  5. ADT Ubuntu X64 下ia32-libs替换等【待编辑】

    sudo apt-get install ia32-libs apt-get install libglib2.0-0:i386 libpng12-0:i386 libsm6:i386 libxren ...

  6. PHP 学习笔记---基本语法

    ------php语言与JavaScript的使用 方法是相似 <script type="text/javascript"> </script>--js与 ...

  7. mysql performance_schema 和information_schema.tables了解

    这个是关于mysql的系统表,性能表,核心表操作的一些介绍,深入算不上 我们一般很少去动 mysql  information_schema 信息相关  performance_schema 性能相关 ...

  8. Codeforces Round #383 (Div. 2) D 分组背包

    给出一群女孩的重量和颜值 和她们的朋友关系 现在有一个舞台 ab是朋友 bc是朋友 ac就是朋友 给出最大承重 可以邀请这些女孩来玩 对于每一个朋友团体 全邀请or邀请一个or不邀请 问能邀请的女孩的 ...

  9. asp.net identity 3.0.0 在MVC下的基本使用 序言

    本人也尚在学习使用之中,错误之处请大家指正. 开发环境:vs2015 UP1   项目环境:asp.net 4.6.1   模板为:asp.net 5 模板     identity版本为:asp.n ...

  10. NC57,NC63-NC二开经验总结

    版主2010级市场营销专业本科生 2013年8月入达内培训Java相关技术 12月入职,做用友NC的二次开发工作 2015年4月离职,4中下旬入职一家互联网金融企业 下面是做NC二开期间积累的一些常用 ...