题目描述:

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. python 获取日期

    转载   原文:python 获取日期 作者:m4774411wang python 获取日期我们需要用到time模块,比如time.strftime方法 time.strftime('%Y-%m-% ...

  2. 一步一步打造自己的Android图片浏览器(原创)

    今天我们试着来制作一个自己的Android图片浏览器. 图片浏览器应该具有什么功能呢?鉴于不同的人不同的理解,这里提出一个基本的需求: 搜索手机内的所有图片,展示于一个列表中: 列表中展示的是图片的缩 ...

  3. Tomcat 解压版安装

    1.下载tomcat7.0 http://tomcat.apache.org/download-70.cgi

  4. NOI 题库 8465

    8465  马走日 描述 马在中国象棋以日字形规则移动. 请编写一段程序,给定n*m大小的棋盘,以及马的初始位置(x,y),要求不能重复经过棋盘上的同一个点,计算马可以有多少途径遍历棋盘上的所有点. ...

  5. NOIP2015D1

    好像来的有点晚,但我的确现在刚做这套题 T1神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可 ...

  6. 利用OTP为odoo增强安全访问

    两次验证是广泛应用于各大站点的验证机制,我们今天利用Google Authentication来实现Odoo的两次验证,防止撞库或密码泄露等引起的安全问题. 1. 二次验证的原理 参见 http:// ...

  7. PowerGUI错误-Microsoft SharePoint is not supported with version 4 of the Microsoft .Net Runtime

    PowerGUI是个写powershell的神器,相比于PowerShell ISE,它那断点和按步追踪的能力不知让多少脚本狂们神魂颠倒.. 今天我也下载了一个放到测试环境里打算玩玩,结果出师不利,一 ...

  8. ComboSelect 下拉筛选

    Jquery Combo Select下拉筛选 http://www.dowebok.com/179.html

  9. equals和==的区别

    ---恢复内容开始--- equals:用于判断两个变量是否是对同一个对象的引用,即堆中的内容是否相同. 1.第一:对象不同,内容相同: ==:等于.比较两个地址是不是一样的(地址一样值肯定一样)(比 ...

  10. linux文件数相关命令

    查看系统目前打开的文件数命令#cat /proc/slabinfo | grep ip_conn | grep -v ip_conntrack_expect | awk '{print $2}' 查看 ...