HDU 5918 Sequence I KMP
Sequence I
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).
6 3 1
1 2 3 1 2 3
1 2 3
6 3 2
1 3 2 2 3 1
1 2 3
Case #2: 1
题意:
给你a,b两个序列
和一个p ,求有多少个 q恰好满足 b1,b2,b3....bm 就是 a[q],a[q+p],a[q+2p]......a[q+(m-1)p];
题解:
将a序列,每隔p位置分成一组,这样最多有p组,个数和是n
将每组和b序列跑KMP计算答案
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 1e6+, M = 1e6, mod = 1e9+, inf = 2e9; int T,n,m,p,s[N],t[N],ans = ;
vector<int > P[N];
int nex[N];
int main()
{
int cas = ;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&p);
for(int i = ; i <= n; ++i) scanf("%d",&t[i]);
for(int i = ; i <= m; ++i) scanf("%d",&s[i]);
for(int i = ; i < p; ++i) P[i].clear();
memset(nex,,sizeof(nex));
ans = ;
int k = ;
for(int i=; i<=m; i++)
{
while(k>&&s[k+]!=s[i]) k = nex[k];
if(s[k+]==s[i])k++;
nex[i] = k;
}
if(p == )
{
k = ;
for(int i=; i<=n; i++)
{
while(k>&&s[k+]!=t[i]) k = nex[k];
if(s[k+]==t[i]) k++;
if(k==m) {
k = nex[k];
ans++;
}
}
printf("Case #%d: ",cas++);
printf("%d\n",ans);
}
else {
for(int i = ; i <= n; ++i)
P[i % p].push_back(t[i]);
for(int i = ; i < p; ++i) {
k = ;
for(int j = ; j < P[i].size(); ++j) {
while(k>&&s[k+]!=P[i][j]) k = nex[k];
if(s[k+]==P[i][j]) k++;
if(k==m) {
k = nex[k];
ans++;
}
}
}
printf("Case #%d: ",cas++);
printf("%d\n",ans); }
}
}
HDU 5918 Sequence I KMP的更多相关文章
- HDU 1711Number Sequence【KMP模板题】
<题目链接> 题目大意: 意思是给出两个串,找出匹配串在模式串中的位置. 解题分析: KMP算法模板题. #include <cstdio> #include <cstr ...
- 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 ...
- 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变 ...
- hdu 5510 Bazinga(字符串kmp)
Bazinga Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- 【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 1711 Number Sequence (KMP简单题)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...
- HDU - 1711 A - Number Sequence(kmp
HDU - 1711 A - Number Sequence Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1 ...
- HDU 1711 Number Sequence(KMP)附带KMP的详解
题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...
随机推荐
- Python中下划线的使用方法
本文将讨论Python中下划线(_)字符的使用方法.我们将会看到,正如Python中的很多事情,下划线的不同用法大多数(并非所有)只是常用惯例而已. 单下划线(_) 通常情况下,会在以下3种场景中使用 ...
- 1.把二元查找树转变成排序的双向链表[BST2DoubleLinkedList]
[题目]:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表.要求不能创建任何新的结点,只调整指针的指向. 比如将二元查找树 . 10 / \ 6 14 / \ / \ 4 8 12 16 转 ...
- java一维数组
1.通过数组名进行赋值,其实质是引用 比如数组array1和数组array2 若执行array2=array1,实际上将array1的引用传递给array2,array1和array2 最后都指向同一 ...
- OpenNI结合Unity3D Kinect进行体感游戏开发(转)
OpenNI结合Unity3D Kinect进行体感游戏开发(转) 楼主# 更多 发布于:2012-07-17 16:42 1. 下载安装Unity3D(目前版本为3.4)2. 下载OpenN ...
- UIWebView内嵌网页 Xcode7.0以后的用法
UIWebView* webPage=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight-64)]; ...
- mysql格式化整数类型时间生成年月日时分秒格式(long或string接收)
数据库格式: 数据库mysql语句: FROM_UNIXTIME( s.timemodified, '%Y-%m-%d %h:%i:%s' ) 生成结果: 测试sql为: SELECT *, FROM ...
- struts2文件上传和下载
1. struts系统中的拦截器介绍 过滤器:javaweb中的服务器组件,主要针对的请求和响应进行拦截. 拦截器:主要针对方法的调用,进行拦截器,当使用代理对象调用某个方法时候 对方法的调用进行拦截 ...
- mac os 下打开FTP服务器
mac下一般用smb服务来进行远程文件访问,但要用FTP的话,高版本的mac os默认关掉了,可以用如下命令打开: sudo -s launchctl load -w /System/Library/ ...
- ios滑动手势全屏(这段代码实现了下一级控制器滑到上一级控制器)
在自定义导航控制器里面加以下代码就增加全屏滑动手势 >推向前一个控制器 // HBNavigationController.m // #import "HBNavigationCon ...
- 在Eclipse中创建Maven多模块工程的例子
.配置eclipse的maven .在Eclipse里面New -> Maven Project -> 选择“Create a simple project” Group Id: com. ...