题目描述:

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. PHP 删除目录及目录下文件

    <?php function del_dir ($dir,$type=true){    $n=0;    if (is_dir($dir)) {        if ($dh = opendi ...

  2. linux 查找文件或者内容常用命令

    whereis <程序名称> find [路径] <表达式> locate <文件名称> 从文件内容查找匹配指定字符串的行: $ grep "被查找的字符 ...

  3. iOS--UIScrollView图片动画切换【实现每次只加载3张图片,进而减少占用内存,可循环滚动】

    #import "ViewController.h" #define IMAGENUMBER 5 #define SIZE self.view.bounds.size @inter ...

  4. sql 动态行转列

    create table u01 (医案编号 varchar(5),药物编号 varchar(5)) insert into u01 select '01','01' union all select ...

  5. mysql刷日志的两个参数

    innodb_flush_log_at_trx_commit 0:每秒 ----log---disk1:事物提交 ---log ---disk2:事物提交---log 每秒 ---disk sync_ ...

  6. asp.net在线预览txt文件(简单实现)

    最近在做文件的在线预览,发现txt文件没有一个较好的方法去实现,想了想可能是比较简单就直接在后台输出了 txt文件

  7. cout中的执行顺序_a++和++a

    printf和cout从右到左计算: int main() { /* char* str = NULL; setmemory(&str, 100); strcpy(str, "hel ...

  8. Android-简单拨号器案例

    Android [19]简单电话拨号器 @方法步骤 1.新建一个android程序,项目名设置为 phone  ,然后打开  phone->res->layout->activity ...

  9. T2 Func<in T1,out T2>(T1 arg)

    委托调用方法的4种方式. using System; using System.Collections.Generic; namespace ConsoleApplication1 { delegat ...

  10. 好看的IDE配色方案让代码看起来不再那么凶猛了

    写这篇小文的初衷是,笔者是原教旨主义者,一直坚持用IDE默认的配色方案.另外也觉得网上黑色系的配色方案太过bling bling了.但今天尝试用新的配色方案后,兴奋地发现对代码的好感度大幅提升. 嗯, ...