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

Total Submission(s): 706 Accepted Submission(s): 266

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

2016中国大学生程序设计竞赛(长春)-重现赛

【题解】



意思是让你在

a1,a1+p,a1+2p,a1+3p…

a2,a2+p,a2+2p,a2 + 3p..

a3,a3+p,a3+2p,a3 +3p



ap,ap+p,ap+2p,ap+3p..

(a右边的东西都是下标;)

这p个序列里面找b数组的匹配数目;

用vector类处理出这个p个数列就好。

剩下的用KMP算法解决。

找完一个匹配之后,j==m。

这个时候让j= f[j];

就能继续找匹配了。

记住就好。不然每次都想好烦。

#include <cstdio>
#include <iostream>
#include <vector> const int MAXN = 2e6;
const int MAXM = 2e6; using namespace std; int p;
vector <int> a[MAXN];
int b[MAXM];
int f[MAXM],ans,n,m; void input(int &r)
{
int t = getchar();
while (!isdigit(t)) t = getchar();
r = 0;
while (isdigit(t)) r = r * 10+t-'0', t = getchar();
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
int t;
input(t);
for (int q = 1; q <= t; q++)
{
for (int i = 0; i <= 1000000; i++)//a[0]也要clear!
a[i].clear();
ans = 0;
input(n); input(m); input(p);
for (int i = 1; i <= n; i++)
{
int x;
input(x);
a[(i%p)].push_back(x);
}
for (int j = 0; j <= m - 1; j++)
input(b[j]);
f[0] = 0; f[1] = 0;
for (int i = 1; i <= m - 1; i++)//获取失配函数,b数组下表是从0开始的。
{
int j = f[i];
while (j && b[j] != b[i]) j = f[j];
f[i + 1] = b[j] == b[i] ? j + 1 : 0;
}
for (int i = 0; i <= p - 1; i++)//给p个数列找匹配数目
{
int j = 0, len = a[i].size();
for (int k = 0; k <= len - 1; k++)
{
while (j && a[i][k] != b[j]) j = f[j];
if (a[i][k] == b[j])
j++;
if (j == m)
{
ans++;
j = f[j];
}
}
}
printf("Case #%d: %d\n", q, ans);
}
return 0;
}

【37.68%】【hdu 5918】Sequence I的更多相关文章

  1. 【hdu 5918】Sequence I(KMP)

    给定两个数字序列,求a序列中每隔p个构成的p+1个序列中共能匹配多少个b序列. 例如1 1 2 2 3 3 每隔1个的序列有两个1 2 3 kmp,匹配时每次主串往前p个,枚举1到p为起点. 题目 # ...

  2. 【改革春风吹满地 HDU - 2036 】【计算几何-----利用叉积计算多边形的面积】

    利用叉积计算多边形的面积 我们都知道计算三角形的面积时可以用两个邻边对应向量积(叉积)的绝对值的一半表示,那么同样,对于多边形,我们可以以多边形上的一个点为源点,作过该点并且过多边形其他点中的某一个的 ...

  3. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  4. Least Common Multiple (HDU - 1019) 【简单数论】【LCM】【欧几里得辗转相除法】

    Least Common Multiple (HDU - 1019) [简单数论][LCM][欧几里得辗转相除法] 标签: 入门讲座题解 数论 题目描述 The least common multip ...

  5. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. 【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5489 题目大意: 一个N(N<=100000)个数的序列,要从中去掉相邻的L个数(去掉整个区间 ...

  7. 【贪心】【模拟】HDU 5491 The Next (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意: 一个数D(0<=D<231),求比D大的第一个满足:二进制下1个个数在 ...

  8. 【动态规划】【二分】【最长上升子序列】HDU 5773 The All-purpose Zero

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5773 题目大意: T组数据,n个数(n<=100000),求最长上升子序列长度(0可以替代任何 ...

  9. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

随机推荐

  1. android Email总结文档

    目录:src\com.android.email.activity 一. Welcome.java 根据AndroidManifest.xml可知该文件为程序入口文件: 加载该文件时,查询数据库账户列 ...

  2. (转)bat批处理的注释语句

    在批处理中,段注释有一种比较常用的方法: goto start = 可以是多行文本,可以是命令 = 可以包含重定向符号和其他特殊字符 = 只要不包含 :start 这一行,就都是注释 :start 另 ...

  3. 微服务实战(六):选择微服务部署策略 - DockOne.io

    原文:微服务实战(六):选择微服务部署策略 - DockOne.io [编者的话]这篇博客是用微服务建应用的第六篇,第一篇介绍了微服务架构模板,并且讨论了使用微服务的优缺点.随后的文章讨论了微服务不同 ...

  4. C语言中 / 得到的结果

  5. 数据库中暂时表,表变量和CTE使用优势极其差别

    1 在写SQL时常常会用到暂时表,表变量和CTE,这三者在使用时各有优势: 1. 暂时表:分为局部暂时表和全局暂时表. 1.1局部暂时表,创建时以#开头,在系统数据库tempdb中存储. 在当前的链接 ...

  6. 在sublime text 3中安装中文支持 分类: C_OHTERS 2015-04-24 22:04 229人阅读 评论(0) 收藏

    1.安装package control 使用control+~打开终端,然后输入以下内容并确定: import  urllib.request,os;pf='Package Control.subli ...

  7. ZOJ 1494 Climbing Worm 数学水题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=494 题目大意: 一只蜗牛要从爬上n英寸高的地方,他速度为u每分钟,他爬完u需要 ...

  8. POJ 2642 The Brick Stops Here 0-1背包

    poj: http://poj.org/problem?id=2642 大意: 给出n(n<=200)块黄铜合金,还有它们的浓度和价钱.给出若干个个询问使它们在n块中取 M 块 使得这M块合金的 ...

  9. ZOJ 1136 Longest Ordered Subsequence DP

    传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1136 题目大意:给定一串序列,求最长的升序列长度,如1, 7, 3, ...

  10. js 99乘法表

    哈哈哈,笑死我了,突然怀念学习时代,撸了一个乘法表 for(let a=1;a<10;a++){let str = ''; for(let b=1;b<10;b++){ str = str ...