Sequence I (hdu 5918)

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1938    Accepted Submission(s): 730

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,⋯,bmis 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

//题意: 字符串匹配,就是,n 长主串,m 长匹配串,k 长间隔,问有多少种匹配?

分成 k 组就好,这题可以用来测测你的 KMP 模板哦,数据还可以,就是,就算是朴素匹配也能过。。。

做完我算是真的理解KMP了,对于字符串,有个 \0 结尾的特性,所以优化的 next 是可行的,但是这种却不行,而且,优化的并不好求匹配数。

KMP 模板 : http://www.cnblogs.com/haoabcd2010/p/6722073.html

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
#define MX 1000005 int n,m,p;
int ans;
int t[MX];
vector<int> zu[MX];
int net[MX]; void Init()
{
for (int i=;i<=n;i++)
zu[i].clear();
} void get_next()
{
int i=,j=-;
net[]=-;
while (i<m)
{
if (j==-||t[i]==t[j]) net[++i]=++j;
else j = net[j];
} for (int i=;i<=m;i++)
printf("%d ",net[i]);
printf("\n");
} void KMP(int x)
{
int i=,j=;
int len = zu[x].size();
while(i<len&&j<m)
{
if (j==-||zu[x][i]==t[j])
{
i++,j++;
}
else j=net[j];
if (j==m)
{
ans++;
j = net[j];
}
}
} int main()
{
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
scanf("%d%d%d",&n,&m,&p);
Init();
for (int i=;i<n;i++)
{
int x;
scanf("%d",&x);
zu[i%p].push_back(x);
}
for (int i=;i<m;i++)
scanf("%d",&t[i]);
get_next(); ans = ;
for (int i=;i<p;i++) KMP(i);
printf("Case #%d: %d\n",cas,ans);
}
return ;
}

Sequence I的更多相关文章

  1. oracle SEQUENCE 创建, 修改,删除

    oracle创建序列化: CREATE SEQUENCE seq_itv_collection            INCREMENT BY 1  -- 每次加几个              STA ...

  2. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  3. DG gap sequence修复一例

    环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...

  4. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  6. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  7. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  8. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  10. Leetcode 60. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

随机推荐

  1. linux 修改时间

    实例:设置时间伟2008年8月8号12:00# date -s "2008-08-08 12:00:00"修改完后,记得执行clock -w,把系统时间写入CMOS date -s ...

  2. openerp wizard to tree view

    从wizard跳转到tree视图最简单的方法   <act_window context="{'search_default_server_id': active_id, 'defau ...

  3. hdu1236 排名(结构体排序)

    排名 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

  4. 【Python3 爬虫】11_报错No module named 'requests'

    从网上下载了一段源码,执行过程中报错: No module named 'requests' 一看英文就明白是咋回事了~ 是由于:没有模块requests 解决方案 打开cmd,在窗口运行命令:pip ...

  5. 【Java】Java_17 数组

    数组 数组是一种数据类型,属于引用类型. 1.定义数组 type[] arrayName; type arrayNmae[]; 以上2种定义数组方式的区别: type[] arrayName:语义强, ...

  6. (转)jquery图片左右滚动

    <!DOCTYPE HTML> <html> <head> <title>基于jQuery的控制左右滚动效果_自动滚动版本</title> ...

  7. iOS 转盘动画效果实现

    代码地址如下:http://www.demodashi.com/demo/11598.html 近期公司项目告一段落,闲来无事,看到山东中国移动客户端有个转盘动画挺酷的.于是试着实现一下,看似简单,可 ...

  8. C语言学习笔记(二) 基础知识

    数据类型 C语言数据可以分为两大类: 基本类型数据和复合类型数据: 基本类型数据 整数 整型   (int)  ——占4字节 短整型(short int)  ——占2字节    长整型(long in ...

  9. android 静态和动态设置 Receiver的 android:enabled值

    0x 01 前提约束: 0x001 静态检查:指用action限定Intent,并使用包管理器的queryBroadCastReceivers方法,在flags字段置为0时查找ResolveInfo, ...

  10. 读写文件,用代码在讲html文件转为jsp文件

    package my.testguava; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputSt ...