代码

 #include <iostream>
#include <string>
#include <typeinfo>
#include <vector>
using namespace std; int KMP(const string &pattern,const string &str)
{
const int len1 = pattern.length();
const int len2 = str.length();
vector<int> next(len1);
next[]=;
int i,j,k;
for (i=;i<len1;i++)
{
k=next[i-];
while (pattern[i] != pattern[k]&&k!=)
{
k=next[k-];
}
if(pattern[i] == pattern[k])
next[i]=k+;
else
next[i]=;
}
i=j=;
int mycount=;
while (j < len2)
{
if (pattern[i]!=str[j])
{
if (i==)
{
i=;
j++;
}
else
i=next[i-];
}
else
{
i++;
j++;
if(pattern[i] == '\0')
{
mycount++;
i=next[len1-];//为什么取pattern最后一个字符的next,自己想
//i赋值位next[len1-1]是为了防止j回溯
}
}
} return mycount;
} int main()
{
int n;
string pattern,str;
while (cin >> n)
{
while (n--)
{
cin >> pattern >> str;
cout << KMP(pattern,str)<<endl;
}
}
return ;
}

hihocoder 1015题的更多相关文章

  1. hihoCoder #1015 : KMP算法【KMP裸题,板子】

    #1015 : KMP算法 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在 ...

  2. hihoCoder 1015 KMP算法(kmp)

    #1015 : KMP算法 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣.他们约定好互相帮助 ...

  3. 杭电acm 1015题

    马上要找工作了,锻炼下自己的写程序能力,不多说,上代码 /********************杭电acm 1015 已AC 在这个程序里,使用穷举法来实现,但是输出顺序需要安装字典的最大 来输出 ...

  4. hihocoder 1015 KMP(找多个位置的 【*模板】)

    #1015 : KMP算法 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在 ...

  5. hihoCoder#1105 题外话·堆

    原题地址 有没有更优雅地堆模板啊,总感觉我写的有些啰嗦 代码: #include <iostream> using namespace std; #define MAX_NODE 1000 ...

  6. hihoCoder#1015 : KMP算法 (KMP模板)

    代码如下: # include<iostream> # include<cstdio> # include<cstring> # include<vector ...

  7. hihoCoder 1015 KMP算法

    题意:经典字符串匹配算法.给定原串和模式串,求模式串在原串中出现的次数.算法讲解 AC代码 #include <cstdio> #include <cmath> #includ ...

  8. hihocoder #1015 KMP

    #include<stdio.h> #include<iostream> #include<math.h> #include<string.h> usi ...

  9. hihocoder刷题 扫雷游戏

    题目1 : 扫雷游戏 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个N × N的方格矩阵,其中每个格子或者是'*',表示该位置有一个地雷:或者是'.',表示该位 ...

随机推荐

  1. Ansible系列(六):循环和条件判断

    本文目录:1. 循环 1.1 with_items迭代列表 1.2 with_dict迭代字典项 1.3 with_fileglob迭代文件 1.4 with_lines迭代行 1.5 with_ne ...

  2. 自定义Django的中间件

    分析Django的生命周期,我们知道所有的http请求都要经过Django的中间件. 假如现在有一个需求,所有到达服务端的url请求都在系统中记录一条日志,该怎么做呢? Django的中间件的简介 D ...

  3. HDU2688-Rotate

    Recently yifenfei face such a problem that give you millions of positive integers,tell how many pair ...

  4. Oracle DBA 常用查询

    1. 查询系统所有对象 select owner, object_name, object_type, created, last_ddl_time, timestamp, statusfrom db ...

  5. asp.net web api 2.2 基础框架(带例子)

    链接:https://github.com/solenovex/asp.net-web-api-2.2-starter-template 简介 这个是我自己编写的asp.net web api 2.2 ...

  6. Redis info 参数详解

    Redis Info 命令以一种易于理解和阅读的格式,返回关于 Redis 服务器的各种信息和统计数值. 通过给定可选的参数 section ,可以让命令只返回某一部分的信息: server : 一般 ...

  7. [原创]KVM虚拟化管理平台的实现

    KVM虚拟化管理平台的实现 源码链接:https://github.com/wsjhk/IaaS_admin.git 根据KVM虚拟化管理的要求,设计并实现网页操作管理KVM虚拟机.设计原理架构如下图 ...

  8. Hadoop就是一个别人造好的轮子

    这个想法源自于我看了<Hadoop: The Definitive Guide>的Part I Ch 2中MapReduce的引入和介绍,书中先说了怎么通过原始的办法处理数据,然后引入到如 ...

  9. Python自学笔记-filter()函数(来自廖雪峰的官网Python3)

    感觉廖雪峰的官网http://www.liaoxuefeng.com/里面的教程不错,所以学习一下,把需要复习的摘抄一下. 以下内容主要为了自己复习用,详细内容请登录廖雪峰的官网查看. Python内 ...

  10. Echarts数据可视化series-heatmap热力图,开发全解+完美注释

    全栈工程师开发手册 (作者:栾鹏) Echarts数据可视化开发代码注释全解 Echarts数据可视化开发参数配置全解 6大公共组件详解(点击进入): title详解. tooltip详解.toolb ...