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. ural1439 Battle with You-Know-Who

    Battle with You-Know-Who Time limit: 2.0 secondMemory limit: 64 MB Rooms of the Ministry of Magic ar ...

  2. eclipse代码提示配置

    打开 MyEclipse 6.5,然后“window”→ “Preferences” 2.选择“java”, 展开, “Editor”, 选择“Content Assist”. 3.选择“Conten ...

  3. ComboBox值排序

    ComboBox值排序先是想通过冒泡排序,但是冒泡排序是int类型,又打算通过下标,进行字符串排序,然后想到了一个简单的办法……先存入ArrayList排序Sort清空ComboBox再遍历存入 ...

  4. selenium 多线程

    http://www.cnblogs.com/dingmy/p/3438084.html

  5. mysql用存储过程插入百万条数据, 及查询优化

    查看所有存储过程: show procedure status; 查看详细存储过程 ptest: show create procedure ptest; 存储过程插入数据: create table ...

  6. OPENCV图像变换-2

    一.经典霍夫变换 霍夫变换是图像处理中的一种特征提取技术,该方法通过在一个参数空间中通过计算累计结果的局部最大值来得到一个符合该特定形状的集合,作为结果. 运用两个坐标空间之间的变换,将一个空间中具有 ...

  7. Spark1.3.0安装

    之前在用Hadoop写ML算法的时候就隐约感觉Hadoop实在是不适合ML这些比较复杂的算法.记得当时写完kmeans后,发现每个job完成后都需要将结果放在HDFS中,然后下次迭代的时候再从文件中读 ...

  8. ubuntu如何安装Mac主题

    1.安装 Gnome 经典桌面 sudo apt-get install gnome-session-fallback 没有安装桌面的可安装 Gnome 桌面: sudo apt-get instal ...

  9. delphi显示hello world 和退出程序

    Label1.Caption:='hello world!' Form1.close; application.Terminate; //终止程序 Application.Run; //程序运行 te ...

  10. 超市RFID结算系统项目进度与总结

    超市RFID结算系统项目进度与总结 超市RFID结算系统本周末(明天演示),目前进度如下: 一.后台PHP端已经完成了大部分的工作,包括以下: 1.数据库的建立(目前只包括用户表.商品信息表.购物车表 ...