leetcode 28
题目描述:
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的更多相关文章
- 前端与算法 leetcode 28.实现 strStr()
# 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和 ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- Java实现 LeetCode 28 实现strStr()
28. 实现 strStr() 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ...
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- LeetCode 28 Implement strStr() (实现找子串函数)
题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description Problem : 实现找子串的操作:如果没有找到则返回 ...
- LeetCode(28)题解:Implement strStr()
https://leetcode.com/problems/implement-strstr/ 题目: Implement strStr(). Returns the index of the fir ...
- <每日 1 OJ> -LeetCode 28. 实现 strStr()
题目: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存 ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
随机推荐
- IE8+兼容经验小结
最近一段时间,我都使用Flask+Bootstrap3的框架组合进行开发.本文就是在这种技术组合下,分享IE8+兼容性问题的解决方法.根据我的实践经验,如果你在写HTML/CSS时候是按照W3C推荐的 ...
- SpringMVC注解@RequestParam全面解析---打酱油的日子
在SpringMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取.这里主要 ...
- 解决Win8无法升级.NET Framework 3.5.1 提示错误0x800F0906
搞了好久,发现了这篇文,很清晰,就引用了过来.http://www.xdowns.com/article/239/Article_3065.html 起因是windows8.1装oracle10g提示 ...
- SQL 执行进展优化
聚集索引扫描 SELECT * FROM C_SY_Ownedstorm 聚集索引扫描比表扫描快 聚集索引扫描:发生于聚集表,也相当于全表扫描操作,但在针对聚集列的条件等操作时,效率会较好. 表扫描 ...
- Swift微博编写感
首先Swift是苹果2014年力推的编程语言.可见发展趋势 在此提供
- Oracle12c IMO 测试
1.概述 2014 年 6 月,在 Oracle 12c 的 12.1.0.2 版本中,Oracle 正式发布和引入了基于内存和列式计算的 In-Memory Option 组件 (以下简称 IMO) ...
- c++代码中,使用svn版本号作为程序版本号的实现方法
1.编写版本模板文件 #ifndef _VERSIONSVN_H_#define _VERSIONSVN_H_#define VER_REVISIONSVN $WCREV$#endif //!_VER ...
- run time
http://www.cnblogs.com/yswdarren/p/3619303.html
- .net core 1.0 中的asp.net identity 的基本使用 序言
2016年6月底,微软发不了vs2015 up3,在这个版本中,微软做了一些改变,本人目前也尚在学习使用之中,现把学习和使用的心得写出来,错误之处请大家指正. 开发环境:vs2015 UP3 项目 ...
- Apache服务器性能监控
Apache服务器性能监控 1.使用自带mod_status模块监控 1)加载mod_status.so 模块 在httpd.conf中打开LoadModule status_module modul ...