Sequence I

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 729    Accepted Submission(s): 277

Problem Description

Mr. Frog has two sequences a1,a2,⋯,an and b1,b2,⋯,bm and a number p. He wants to know the number of positions q such that sequence b1,b2,⋯,bm is exactly the sequence aq,aq+p,aq+2p,⋯,aq+(m−1)p where q+(m−1)p≤n and q≥1.
 

Input

The first line contains only one integer T≤100, which indicates the number of test cases.

Each test case contains three lines.

The first line contains three space-separated integers 1≤n≤106,1≤m≤106 and 1≤p≤106.

The second line contains n integers a1,a2,⋯,an(1≤ai≤109).

the third line contains m integers b1,b2,⋯,bm(1≤bi≤109).

 

Output

For each test case, output one line “Case #x: y”, where x is the case number (starting from 1) and y is the number of valid q’s.
 

Sample Input

2
6 3 1
1 2 3 1 2 3
1 2 3
6 3 2
1 3 2 2 3 1
1 2 3
 

Sample Output

Case #1: 2
Case #2: 1
 

Source

 
 题意:问从a串中每隔p取字,有多少个可与b串匹配
思路:用kmp可计算连续的a串中有多少个b串,可将每p个数字取出组成连续的新串,共可取出p条新串,对每条新串kmp再求和即可
 //2016.10.08
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std;
const int N = ;
const int inf = 0x3f3f3f3f;
int a[N], b[N], nex[N],n, m, p; void pre_kmp(int b[], int m)//得到next数组
{
int i, j;
j = nex[] = -;
i = ;
while(i < m)
{
while(j != - && b[i]!= b[j])j = nex[j];
nex[++i] = ++j;
}
} int kmp(int a[], int n, int b[], int m)
{
int ans = ;
pre_kmp(b, m);
for(int pos = ; pos < p; pos++)
{
int i = pos, j = ;
while(i < n)
{
while(j != - && a[i] != b[j])j = nex[j];
i += p; j++;
if(j >= m){ans++; j = nex[j];}
}
}
return ans;
} int main()
{
int T, kase = ;
scanf("%d", &T);
while(T--)
{
scanf("%d%d%d", &n, &m, &p);
for(int i = ; i < n; i++)scanf("%d", &a[i]);
for(int i = ; i < m; i++)scanf("%d", &b[i]);
printf("Case #%d: %d\n", ++kase, kmp(a,n,b,m));
} return ;
}

HDU5918(KMP)的更多相关文章

  1. HDU5918【KMP大法好,虽然我不会】

    #include <bits/stdc++.h> using namespace std; typedef long long LL; const; int n,m; int a[MAX] ...

  2. KMP算法求解

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

  3. 简单有效的kmp算法

    以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...

  4. KMP算法

    KMP算法是字符串模式匹配当中最经典的算法,原来大二学数据结构的有讲,但是当时只是记住了原理,但不知道代码实现,今天终于是完成了KMP的代码实现.原理KMP的原理其实很简单,给定一个字符串和一个模式串 ...

  5. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  6. [KMP]【学习笔记】

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36916   Accepted: 14904 Descript ...

  7. KMP算法实现

    链接:http://blog.csdn.net/joylnwang/article/details/6778316 KMP算法是一种很经典的字符串匹配算法,链接中的讲解已经是很明确得了,自己按照其讲解 ...

  8. KMP专题

    1.[HDU 3336]Count the string(KMP+dp) 题意:求给定字符串含前缀的数量,如输入字符串abab,前缀是a.ab.aba.abab,在原字符串中出现的次数分别是2.2.1 ...

  9. KMP学习之旅

    说起kmp就要从字符串的匹配说起,下面我们谈谈字符串的匹配 给定一个原字符串:bababababababababb,再给定一个模式串:bababb,求模式串是否在源字符串中出现 最简单的方法就是遍历源 ...

随机推荐

  1. PAT (Advanced Level) 1079. Total Sales of Supply Chain (25)

    树的遍历. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  2. C缺陷与陷阱----读书笔记---第一章

    第一章:词法陷阱 编译器中负责将程序分解为一个一个符号的部分,一般称为“词法分析器”.例如,对于语句: if ( x == big ) big = x ; 它的第一个符号是C语言关键字if,紧接着下一 ...

  3. SpringMVC强大的数据绑定(2)——第六章 注解式控制器详解

    SpringMVC强大的数据绑定(2)——第六章 注解式控制器详解 博客分类: 跟开涛学SpringMVC   6.6.2.@RequestParam绑定单个请求参数值 @RequestParam用于 ...

  4. IOS开发-UI学习-根据URL显示图片,下载图片的练习(button,textfield,image view,url,data)

    编写一个如下界面,实现: 1.在文本输入框中输入一个网址,然后点击显示图片,图片显示到UIImageView中. 2.点击下载,这张显示的图片被下载到手机的Documents文件夹下的Dowmload ...

  5. orientationchange

    <!DOCTYPE html> <html> <head> <title>OrientationChange Event Example</tit ...

  6. error: Embedded binary's bundle identifier is not prefixed with the parent app's bundle identifier

    xcode + iwatch调试错误 在工程的  Targets 下面的 三项(工程名为my):my . my Watchkit app .my Watchkit extention General ...

  7. linux下简单限制网卡速度

    Linux下限制网卡的带宽,可用来模拟服务器带宽耗尽,从而测试服务器在此时的访问效果. 1.安装iproute yum -y install iproute 2.限制eth0网卡的带宽为50kbit: ...

  8. redis 高级配置

    一.安全性 设置密码:在配置文件中设置 requirepass 123456 由于redis的速度非常快,每秒可以进行15万次的暴力破解,所以密码设置要强壮些 在客户端登录或者连接的时候,使用 aut ...

  9. iOS 之 ARC 的内存泄露

    循环引用导致内存泄露,如block容易内存泄露

  10. iOS 之 #import与#include的区别及@class

    #import 相比#include不会引起交叉编译. @class一般用于头文件中需要声明该类的变量时用到