【37.68%】【hdu 5918】Sequence I
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的更多相关文章
- 【hdu 5918】Sequence I(KMP)
给定两个数字序列,求a序列中每隔p个构成的p+1个序列中共能匹配多少个b序列. 例如1 1 2 2 3 3 每隔1个的序列有两个1 2 3 kmp,匹配时每次主串往前p个,枚举1到p为起点. 题目 # ...
- 【改革春风吹满地 HDU - 2036 】【计算几何-----利用叉积计算多边形的面积】
利用叉积计算多边形的面积 我们都知道计算三角形的面积时可以用两个邻边对应向量积(叉积)的绝对值的一半表示,那么同样,对于多边形,我们可以以多边形上的一个点为源点,作过该点并且过多边形其他点中的某一个的 ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- Least Common Multiple (HDU - 1019) 【简单数论】【LCM】【欧几里得辗转相除法】
Least Common Multiple (HDU - 1019) [简单数论][LCM][欧几里得辗转相除法] 标签: 入门讲座题解 数论 题目描述 The least common multip ...
- 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5489 题目大意: 一个N(N<=100000)个数的序列,要从中去掉相邻的L个数(去掉整个区间 ...
- 【贪心】【模拟】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个个数在 ...
- 【动态规划】【二分】【最长上升子序列】HDU 5773 The All-purpose Zero
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5773 题目大意: T组数据,n个数(n<=100000),求最长上升子序列长度(0可以替代任何 ...
- 【动态规划】【KMP】HDU 5763 Another Meaning
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...
随机推荐
- Android Warning not all local changes may be shown due to an error
idea使用svn出现Warning not all local changes may be shown due to an error,如下图所示: 解决方案: 1.File > Setti ...
- 硬件——nrf51822第一篇,GPIO的使用
未完,待续...... 本实现是基于一个开发箱,包括:综合应用开发系统主板XT-EDU-AK 1套: 手持终端系统 XT-EDU-HK 1套: GPIO操作 工程: 这是一个关于流水灯的程序: 我 ...
- less相关知识点
less是一门css预处理语言,文件后缀名为.less,能减少css文件编写的代码量 官网 http://lesscss.cn/#using-less 安装 使用npm install -g less ...
- JS错误记录 - fgm练习 - 函数传参
<script> window.onload = function() { var oBtn = document.getElementsByTagName('button')[0]; v ...
- 素数表(Eratosthenes)
怎么判断一个数是素数? 常规的方法是枚举从2开始的数,看看是否能被整除. 但是,如果要判断的数很多的时候,那么效率会十分低下.... 一个优化的方法是不用判断比这个数小的所有数(到平方根位置),而是判 ...
- GO语言学习(二)Windows 平台下 LiteIDE 的安装和使用
1. 安装 Go 语言并设置环境变量 参考GO语言学习(一) 2. MinGW 的下载和安装 Windows 下的 Go 调试还需要安装 MinGW. 2.1 下载安装工具的安装 最新版本下载安装工具 ...
- Android中CursorLoader的使用、原理及注意事项
前言 最近在项目中涉及到了即时聊天,因此不可避免地要用到实时刷新的功能,因为在以前的项目中见到别人使用CursorLoader+CursorAdapter+ContentProvider的机制来实现实 ...
- FPGA实现UHS的一些资料
对使用FPGA和SD卡进行UHS模式通信的评估: 论文:基于FPGA的SD UHS-II卡控制器设计与实现 设计IP:SD UHS-II Host Controller 供应商: System Lev ...
- UITextField用法
.创建 .UITextField* textField = [[UITextField alloc]initWithFrame:CGRectMake(, , , )]; .设置委托 //委托类需要遵守 ...
- Activex调试以及m_hWnd为空 解决办法
1. 点击[开始]->[运行] 命令:regedit.2. 定位到HKEY_LOCALMACHINE -> SOFTWARE -> Microsoft -> Internet ...