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/ ...
随机推荐
- linux自动定时备份web程序和mysql数据库
前些天受朋友说linux定时备份不知道怎么搞,叫帮忙处理一下.由于这段时间正闲着,所以也就欣然答应.由于朋友对linux不懂也希望我将操作的过程记录下来,也就是越详细越好.所以写得比较$%^& ...
- unity3D技术之事件函数的执行顺序[转]
unity3D技术之事件函数的执行顺序 转自http://www.yxkfw.com/?p=13703 在unity的脚本,有大量的脚本执行按照预先确定的顺序执行的事件函数.此执行顺序说明如下: ...
- Win7下打开计算机管理时出现错误的解决办法
计算机管理是用户在使用计算机时经常用到的一个选项,可以在桌面上右击计算机,选择"管理"打开.也可以在运行框中输入:compmgmt.msc打开计算机管理程序,里面有常用系统工具如用 ...
- SAP打印出库单需求
*&---------------------------------------------------------------------* *& Report Z_SD_CKD ...
- 【干货】ECS服务器OPENVPN搭建,方便管理所有内网服务器
[干货]ECS服务器OPENVPN搭建,方便管理所有内网服务器 使用场景 一台有外网的ECS服务器+N台无外网的ECS服务器,使用OPENVPN管理全部的ECS服务器(包括无外网的ECS服务器). 鉴 ...
- 【leetcode】Factorial Trailing Zeroes(easy)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- xcode报错,svn : is not a workingCopy
解决方案: 1.点击对应的targets 2.选择build phases 3.在红框处有一个run script选项(截图中已经删除了.),点击下拉按钮,看看是否是与svn有关的东西, 如果是,删除 ...
- 如何查看设备的 UDID
手机连接上电脑,打开 Xcode,确认手机已连上: command+shift+2,就可以查看到 UDID 了:
- Centos以rpm方式进行安装MySql
安装过很多次mysql了,却没好好总结过,每次安装完了都忘,下次还要重新Google,这次总结下,自己以后也有的查. 1.安装采用的的rpm包的方式,安装前要先看系统内是否安装了旧版本的MySql和m ...
- NPOI基本操作XLS
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using Sys ...