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. 下拉列表,点击选择实现跳转链接 onchange="window.location=..."

    <select onchange="window.location=this.value;"> <option value="a.html"& ...

  2. 如何获取用户的地理位置-浏览器地理位置(Geolocation)API 简介

    如何获取用户的地理位置-浏览器地理位置(Geolocation)API 简介 一.总结 一句话总结:Geolocation API(地理位置应用程序接口)提供了一个可以准确知道浏览器用户当前位置的方法 ...

  3. 大话Spark(8)-源码之DAGScheduler

    DAGScheduler的主要作用有2个: 一.把job划分成多个Stage(Stage内部并行运行,整个作业按照Stage的顺序依次执行) 二.提交任务 以下分别介绍下DAGScheduler是如何 ...

  4. Spring5源码深度解析(一)之理解Configuration注解

    代码地址:https://github.com/showkawa/spring-annotation/tree/master/src/main/java/com/brian 1.Spring体系结构 ...

  5. UVA 10603 - Fill BFS~

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&c ...

  6. POJ 2752 Seek the Name, Seek the Fame (KMP)

    传送门 http://poj.org/problem?id=2752 题目大意:求既是前缀又是后缀的前缀的可能的长度.. 同样是KMP,和 HDU 2594 Simpsons' Hidden Tale ...

  7. ORA-00119: invalid specification for system parameter LOCAL_LISTENER;

    错误提示内容及上下文环境: SQL> grant sysdba to weng;grant sysdba to weng*第 1 行出现错误:ORA-01034: ORACLE not avai ...

  8. Bootstrap相关优质项目必备网址

    1:文档全集:这里收集了Bootstrap从V1.0.0版本到现在,整个文档的历史.Bootstrap本身就是一个传奇,而这些文档就是传奇的见证! 官方网址:http://docs.bootcss.c ...

  9. 与Eclipse关于"Call Hierarchy"和"Find Reference"功能比较

    "Call Hierarchy"功能比较 Eclipse的"Call Hierarchy"可以查看一个Java方法或类成员变量的调用树(caller和calle ...

  10. PatentTips - Enhanced I/O Performance in a Multi-Processor System Via Interrupt Affinity Schemes

    BACKGROUND OF THE INVENTION This relates to Input/Output (I/O) performance in a host system having m ...