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 网络图片Url 转 Bitmap

    注意:该方法必须要在子线程中调用,因为涉及网络请求 public Bitmap getBitmap(String url) { Bitmap bm = null; try { URL iconUrl ...

  2. 我的Go语言学习之旅三:Go语言中的for循环

    这个但是为难我了,我一直是走在WEB前的程序猿,各种循环用起来得心应手.突然间发现到了Go这里竟然行不通了,查阅后才发现,原来Go的for是这样写的. for i:=0;i<10;i++{ fm ...

  3. ZJOI2002昂贵的聘礼题解

    http://codevs.cn/problem/1324/ 题目大意 一个部落,你能够和社会地位等级的极差不大于M的全部人交易.你能够拿金币直接从一个人手里买东西,也能够从别人那里买到那个人想要的东 ...

  4. flask的使用(一)

    1.程序基本的说明 #-*-encoding=utf--*- 从flask中引入类 from flask import Flask ,render_template import config 初始化 ...

  5. 配置IP地址及HOSTNAME脚本

    #!/bin/bash #修改IP及HOSTNAME ETHCONF=/etc/sysconfig/network-scripts/ifcfg-eth0 HOSTS=/etc/hosts NETWOR ...

  6. zynq+linux+ramdisk can调试

    由于采用ramdisk文件系统,自带的ip工具版本太旧无法配置can,需要自行编译ip,具体参见参考文献2 1.vivado配置ps 2.设备树增加can0,一般开发板均已提供此配置 can@e000 ...

  7. javascript进阶教程第三章--匿名和闭包--案例实战

    javascript进阶教程第三章--匿名和闭包--案例实战 一.学习任务 通过几个小练习回顾学过的知识点 二.实例 练习1: 实例描述:打开页面后规定时间内弹出一个新窗口,新窗口指定时间后自动关闭. ...

  8. c#List泛型数据扩展,把List&lt;&gt;型数据格式化成List&lt;SelectListItem&gt;,用来作dropdownlist的数据

    代码例如以下 public static List<SelectListItem> CreateSelect<T>(this IList<T> t, string ...

  9. CRT(C Runtime Library)—— C/C++运行时库

    C runtime library(part of the C standard library) 任何一个 C 程序,它的背后都有一套庞大的代码来进行支撑,使得该程序得以运行在更高级别上,而不必担心 ...

  10. Cocos2d-x 3.0final 终结者系列教程05-AppDelegate入口类

    下面是Cocos2d-x的程序入口: class  AppDelegate : private cocos2d::Application { public: AppDelegate(); virtua ...