#include <bits/stdc++.h>
using namespace std;
typedef long long
LL;
const int
MAX=1000010;
int
n,m;
int
a[MAX],b[MAX];
int
nextval[MAX];
LL ans;
void
get_next(int b[],int m)
{
int
i=0;
nextval[0]=-1;
int
j=-1;
while(
i<m)
{
if(
j==-1||b[i]==b[j])
{
++
i;
++
j;
// if(b[i]==b[j])
// nextval[i]=nextval[j];
// else
nextval[i]=j;
}
else

j=nextval[j];
}
}
int
KMP(int a[],int n,int b[],int m,int nextval[],int pos,int p)
{
int
i=pos;
int
j=0;
while(
i<n)
{
if(
j==-1||a[i]==b[j])
{

i+=p;
j++;
if(
j==m)
{

j=nextval[j];
ans++;
}
}
else

j=nextval[j];
}
}
int main()
{
int
t,i,j;
int
cas=1;
scanf("%d",&t);
while(
t--)
{
int
p;
ans=0;
scanf("%d%d%d",&n,&m,&p);
for(
i=0; i<n; ++i)
scanf("%d",&a[i]);
for(
j=0; j<m; ++j)
scanf("%d",&b[j]);
get_next(b,m);
// for(i=0; i<m; i++)
// printf("%d ",nextval[i]);
for(i=0;i<p;i++)
{
if(
i+(m-1)*p>=n)
break;

KMP(a,n,b,m,nextval,i,p);
}

printf("Case #%d: %lld\n",cas++,ans);
}
return
0;
}

HDU5918【KMP大法好,虽然我不会】的更多相关文章

  1. HDU5918(KMP)

    Sequence I Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  2. 2018.09.18 atcoder Best Representation(kmp)

    传送门 思路简单不知为何调试了很久. 显然要么分成n个(所有字符相同),要么分成1个(原字符串无循环节),要么分成两个(有长度至少为2的循环节). 一开始以为可以直接hash搞定. 后来wa了几次之后 ...

  3. [LeetCode] Add Bold Tag in String 字符串中增添加粗标签

    Given a string s and a list of strings dict, you need to add a closed pair of bold tag <b> and ...

  4. 「UVA1328」Period 解题报告

    English题面 题意: 给你一个长度为n的字符串,依次取字符串前i个(前缀),如果前缀由k(k>0)个相同真子串构成,那么输出i和k 直到n为0结束,每组数据后要有一行空白 思路: KMP+ ...

  5. HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. Hash大法

    内容参考<算法竞赛进阶指南> 之前集训的时候听老师讲过,字符串题目中,hash一般不是正解,但是是一个优秀的暴力,可以拿比较多的部分分. hash涉及内容很多,这里只讨论字符串hash 可 ...

  7. KMP——从入门到不会打题

    KMP——从入门到不会打题 前言 如果你不了解哈希,建议先观看本蒟蒻的另一篇博客,对哈希有一定的理解   哈希大法吼 KMP算法,别名烤馍片或者看毛片,由烤馍片男子天团三位神犇同时发现的一种强大的单模 ...

  8. Connect() 2016 大会的主题 ---微软大法好

    文章首发于微信公众号"dotnet跨平台",欢迎关注,可以扫页面左面的二维码. 今年 Connect 大会的主题是 Big possibilities. Bold technolo ...

  9. KMP算法求解

    // KMP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namespac ...

随机推荐

  1. stretchableImageWithLeftCapWidth

    本文转载至 http://www.cnblogs.com/bandy/archive/2012/04/25/2469369.html (NSInteger)topCapHeight 这个函数是UIIm ...

  2. 生成ssh密钥

    打开Git Bash,生成ssh密钥: ssh-keygen -t rsa -C "your_email@youremail.com"

  3. 【BZOJ2339】[HNOI2011]卡农 组合数+容斥

    [BZOJ2339][HNOI2011]卡农 题解:虽然集合具有无序性,但是为了方便,我们先考虑有序的情况,最后将答案除以m!即可. 考虑DP.如果我们已经知道了前m-1个集合,那么第m个集合已经是确 ...

  4. EasyDarwin开源流媒体服务器将select改为epoll的方法

    本文来自EasyDarwin团队Fantasy(fantasy(at)easydarwin.org) 一. EasyDarwin网络模型介绍 EventContext负责监听所有网络读写事件,Even ...

  5. Sparksql 取代 Hive?

    sparksql  hive https://databricks.com/blog/2014/07/01/shark-spark-sql-hive-on-spark-and-the-future-o ...

  6. HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)

    [题目链接]:pid=2444">click here~~ [题目大意]: 给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识.若不能分成两组输出No,否则 ...

  7. C++设计模式实现--策略(Strategy)模式

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/L_Andy/article/details/30489331 一. 举例说明 曾经做了一个程序,程序 ...

  8. 20170319 ABAP 生成XML文件

    方法一:ABAP 使用method方式操作XML 转自:http://www.cnblogs.com/jiangzhengjun/p/4265595.html 方法二:STRANS 转换工具;使用st ...

  9. Ubuntu增加Swap分区大小

    参考:http://blog.csdn.net/mznewfacer/article/details/7334592 以下摘自上述地址内容,并做了点小修改: 1.首先用命令free查看系统内 Swap ...

  10. Python序列——元组

    元组是什么 1 创建元组 2 访问元组中的值 3 更新元组中的元素 4 删除元组中的元素或者元组本身 元组相关操作 内建函数对元组的支持 1 序列类型函数 2 元组内建函数 元组的特殊性 1. 元组是 ...