HDU 5918 Sequence I
题目来源:2016 CCPC 长春站
题意:给出两个序列 a[] , b[] ,如果b1,b2....bm能够与aq,aq+p,aq+2p...aq+(m-1)p对应( q+(m-1)p<=n && q>=1 ),则q为一个合法的数,找出满足题意的q的个数
思路:简单KMP,枚举起点q,如果匹配成功后记录一次然后重新从最后匹配位置进行匹配,一直匹配到a[]结束
/*************************************************************************
> File Name: C.cpp
> Author: WArobot
> Mail: 768059009@qq.com
> Created Time: 2017年04月15日 星期六 21时11分01秒
************************************************************************/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000000+10;
int n,m,p,ans,t;
int a[maxn],b[maxn];
int _next[maxn];
void get__next(){
int plen = m;
_next[0] = -1;
int k = -1 , j = 0;
while(j<plen){
if(k==-1 || b[j]==b[k]) j++ , k++ , _next[j] = k;
else k = _next[k];
}
}
int Kmpserch(int q){
int i = q , j = 0;
int k = 0;
while(i<n && j<m){
if(j==-1 | a[i]==b[j]) i+=p , j++;
else j = _next[j];
if(j==m) k++, j = _next[j];
}
return k;
}
int main(){
int kase=0;
scanf("%d",&t);
while(t--){
ans = 0 ;
scanf("%d%d%d",&n,&m,&p);
for(int i=0;i<n;i++) scanf("%d",a+i);
for(int j=0;j<m;j++) scanf("%d",b+j);
get__next();
for(int q = 0;q<n && q<p ;q++){
ans += Kmpserch(q);
}
printf("Case #%d: %d\n",++kase,ans);
}
return 0;
}
HDU 5918 Sequence I的更多相关文章
- HDU 5918 Sequence I KMP
Sequence I Problem Description Mr. Frog has two sequences a1,a2,⋯,an and b1,b2,⋯,bm and a number p ...
- HDU 3397 Sequence operation(线段树)
HDU 3397 Sequence operation 题目链接 题意:给定一个01序列,有5种操作 0 a b [a.b]区间置为0 1 a b [a,b]区间置为1 2 a b [a,b]区间0变 ...
- 【37.68%】【hdu 5918】Sequence I
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s) ...
- 【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 5919 Sequence II(主席树+逆序思想)
Sequence II Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...
- hdu 5146 Sequence
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5146 Sequence Description Today we have a number sequ ...
- HDU 6395 Sequence 【矩阵快速幂 && 暴力】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others) ...
- hdu 5312 Sequence(数学推导——三角形数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5312 Sequence Time Limit: 2000/2000 MS (Java/Others) ...
- hdu 1711Number Sequence (KMP入门,子串第一次出现的位置)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- .net 技术基础
C#常见运算符 一元运算符(+.-.!.~.++.--) 算术运算符(*./.%.+ . – ) 移位运算符(<< .>> ) 关系和类型测试运算符(==.!=.<.&g ...
- Mysql 忘记数据库密码
windows下忘记MySQL密码的修改方法 1.关闭正在运行的Mysql服务: A.命令下运行 net stop mysql B.找到mysql服务停止mysql的服务 C.在任务管理器中结束M ...
- js:Array对象常用方法介绍
前言 在js中,数组作为一个特殊的对象.是我们常用的数据格式.今天就来梳理一下常用的数组方法. 1.基础 几种基础的就简单介绍一下:创建数组 var arr1 = new Array(); //括号可 ...
- 00064_字符串缓冲区_StringBuffer类
1.StringBuffer类 (1)StringBuffer又称为可变字符序列,它是一个类似于 String 的字符串缓冲区,通过某些方法调用可以改变该序列的长度和内容. (2)tringBuffe ...
- 【 ACM-ICPC 2018 沈阳赛区网络预赛 D】Made In Heaven
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 点可以重复走的k短路. [代码] #include <bits/stdc++.h> #define LL long lo ...
- Docker学习总结(14)——从代码到上线, 云端Docker化持续交付实践
2016云栖大会·北京峰会于8月9号在国家会议中心拉开帷幕,在云栖社区开发者技术专场中,来自阿里云技术专家罗晶(瑶靖)为在场的听众带来<从代码到上线,云端Docker化持续交付实践>精彩分 ...
- HDU5130 Signal Interference
/* HDU5130 Signal Interference http://acm.hdu.edu.cn/showproblem.php?pid=5130 计算几何 圆与多边形面积交 * */ #in ...
- HDU 2138
这题用MILLER测试应该是不可避免的. #include <iostream> #include <cstdio> #include <stdlib.h> #in ...
- [Node.js] Manage Configuration Values with Environment Variables
Storing configuration in files instead of the environment has many downsides, including mistakenly c ...
- ubuntu中写一个shell脚本的过程
gedit hello.sh ,然后输入 #!/bin/bash echo "Hello world!" chmod +x hello.sh ./hello.sh