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) ...
随机推荐
- select Option(增加,删除,清空)
jQuery获取Select选择的Text和Value: $("#select_id").change(function(){//code...}); //为Select添加事件, ...
- struct 模块简介
用处 按照指定格式将Python数据转换为字符串,该字符串为字节流,如网络传输时,不能传输int,此时先将int转化为字节流,然后再发送; 按照指定格式将字节流转换为Python指定的数据类型; 处理 ...
- Civil3D二次开发常见问题总结
Civil3D二次开发常见问题总结 AutoCAD命令提示"未知命令**--"的原因:在Initialize方法内报出异常就会导致这种情况.O__O"-(或是少加了dll ...
- centos solr 部署到 tomcat 上
一.安装 java1.7 环境 链接:https://pan.baidu.com/s/1ti6j9jD-RwUN5xl3bc3ZDw 密码:oc9a 二.下载 tomcat 并解压 链接:https: ...
- Virtual address cache memory, processor and multiprocessor
An embodiment provides a virtual address cache memory including: a TLB virtual page memory configure ...
- navicate11不能激活的问题
navicate11不能激活的问题 学习了:http://blog.csdn.net/sanbingyutuoniao123/article/details/52589678 不要安装在系统盘,如果安 ...
- AlertDialog自己定义View的使用方法+怎样改变弹出框的大小
android系统定义了弹出框,支持我们自己定义布局: public AlertDialog getEditCustomDialog() { LayoutInflater inflater = get ...
- Redis源代码分析(二十二)--- networking网络协议传输
上次我仅仅分析了Redis网络部分的代码一部分,今天我把networking的代码实现部分也学习了一遍,netWorking的代码很多其它偏重的是Clientclient的操作.里面addReply( ...
- Project Euler:Problem 88 Product-sum numbers
A natural number, N, that can be written as the sum and product of a given set of at least two natur ...
- iOS_6_ToolBar+xib+红楼梦
终于效果图 BeyondViewController.h // // BeyondViewController.h // 6_ToolBar // // Created by beyond on 14 ...