28. Implement strStr()

Problem's Link

----------------------------------------------------------------------------

Mean:

给定两个字符串str1和str2,输出str2在str1中第一次出现的下标.

analyse:

KMP算法模板题.

Time complexity: O(N)

view code

/**
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
*       Author: crazyacking
*       Date  : 2016-02-19-16.45
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);

class Solution
{
public:
   int Next[100005];
   void getNext(string& s)
   {
       Next[0]=0;
       for(int i=1,k=0;i<s.length();++i)
       {
           while(s[i]!=s[k] && k)
               k=Next[k-1];
           if(s[i]==s[k])
               ++k;
           Next[i]=k;
       }
   }
   int strStr(string haystack, string needle)
   {
       if(needle.length()==0)
           return 0;
       getNext(needle);
       for(int i=0,k=0;i<haystack.length();++i)
       {
           while(haystack[i]!=needle[k] && k)
               k=Next[k-1];
           if(haystack[i]==needle[k])
               ++k;
           if(k==needle.length())
               return i-k+1;
       }
       return -1;
   }
};

int main()
{
   string str1,str2;
   while(cin>>str1>>str2)
   {
       Solution solution;
       cout<<solution.strStr(str1,str2)<<endl;
   }
   return 0;
}
/*

*/

LeetCode - 28. Implement strStr()相关推荐

  1. 【To Do】LeetCode 28. Implement strStr() 和KMP算法

    LeetCode 28. Implement strStr() Solution1:我的答案 有投机取巧之嫌啊~ 注意string中的查找函数在查找时 参考网址:https://www.cnblogs ...

  2. leetCode 28. Implement strStr() 字符串

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

  3. [leetcode] 28. Implement strStr() 解题报告

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

  4. LeetCode 28 Implement strStr()(实现strStr()函数)

    翻译 实现strStr()函数.返回针(needle)在草垛/针垛(haystack)上第一次出现的索引, 如果不存在其中则返回-1.其实也就是说字符串str2在字符串str1中第一次出现的索引而已. ...

  5. leetcode 28. Implement strStr() 实现strStr()

    C++代码,题目相对不是很难 1 class Solution { 2 public: 3 int strStr(string haystack, string needle) { 4 if(need ...

  6. LeetCode - Easy - 28. Implement strStr()

    Topic Two Pointers, String Description https://leetcode.com/problems/implement-strstr/ Implement str ...

  7. leetcode python3 简单题28. Implement strStr()

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十八题 (1)题目 英文: Implement strStr(). Return ...

  8. 【LeetCode】28. Implement strStr()

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Implement strStr(). Return the index of the fi ...

  9. Leet Code OJ 28. Implement strStr() [Difficulty: Easy]

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

最新文章

  1. Gevent异步服务类实现多姿势WEB实时展示
  2. 通过修改Tomcat配置,解决乱码问题
  3. dataframe转化为array_【Python专栏】12 种高效 Numpy 和 Pandas 函数为你加速分析
  4. java中equals的理解(1)
  5. [Ajax] jQuery中的Ajax -- 04-异步提交表单
  6. java控制台输入汉字_给我一个JAVA控制台输入中文的实例
  7. 云服务器的带宽是如何计算的?
  8. MariaDB ColumnStore一些限制和BUG总结
  9. 机器学习 | 数学基础
  10. OpenSesame for Mac(ae项目格式编辑插件)
  11. 【车间调度】 FJSP的析取图模型
  12. 进程杀手 (prockiller)V2.82绿色版
  13. No3 jQuery
  14. linux挖矿的清理工具,Linux挖矿病毒的清除与分析
  15. 网页端、移动端导航设计模式全解
  16. python提取人物特征_基于图像人物面部表情识别的特征提取优化方法与流程
  17. php 阿里云短信服务及阿里大鱼实现短信验证码的发送
  18. 顺序问题,母版页和内容页
  19. 【Lua】Lua编程相关
  20. #腾讯会议:728-974-0882

热门文章

  1. property_get 与 property_set 的返回值(转载)
  2. The 2014 ACM-ICPC Asia Mudanjiang Regional First Round C
  3. hdu4885 有 限制的最短路
  4. 操作系统原理第九章:虚拟内存
  5. 【Linux 内核】实时调度类 ⑦ ( 实时调度类核心函数源码分析 | dequeue_task_rt 函数 | 从执行队列中移除进程 )
  6. 【Git】Git 标签使用 ( 创建并查询标签 | 推送单个标签到远程仓库 | 推送所有标签到远程仓库 | 删除远程仓库的标签 )
  7. 【Android 逆向】Android 逆向基本概念 ( 定位内存中的修改点 | 基址寻址法 | 搜索定位法 )
  8. 【Android 应用开发】Google 官方 EasyPermissions 权限申请库 ( 简介 | 权限申请处理细节 | 添加依赖 | 界面权限申请结果处理 | 权限申请结果回调接口 )
  9. Struts2 (三)OGLN
  10. ●BZOJ 4596 [Shoi2016]黑暗前的幻想乡