HDU 5918 KMP/模拟
Sequence I
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1013 Accepted Submission(s): 393
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
.
, 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)
.
/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
//typedef long long ll;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
int f[];
void get(int *p,int m)
{
int j=;
f[]=f[]=;
for(int i=;i<m;i++)
{
j=f[i];
while(j&&p[j]!=p[i]) j=f[j];
if(p[i]==p[j]) f[i+]=j+;
else f[i+]=;
}
}
int kmp(int *s,int *p,int n,int m)
{
int num=;
int j=;
for(int i=;i<n;i++)
{
while(j&&p[j]!=s[i]) j=f[j];
if(s[i]==p[j]) j++;
if(j==m) num++;
}
return num;
}
int s[],p[],t[];
int n,m,q;
int main()
{
int T;
scanf("%d",&T);
for(int k=;k<=T;k++)
{
scanf("%d %d %d",&n,&m,&q);
memset(t,,sizeof(t));
memset(p,,sizeof(p));
for(int i=;i<n;i++)
scanf("%d",&t[i]);
for(int i=;i<m;i++)
scanf("%d",&p[i]);
get(p,m);
int ans=;
for(int i=;i<q;i++)
{
int num=;
for(int j=i;j<n&&i+(m-)*q<n;j+=q)
s[num++]=t[j];
ans+=kmp(s,p,num,m);
}
printf("Case #%d: %d\n",k,ans);
}
return ;
}
/*
KMP 处理
*
模拟
/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
//typedef long long ll;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
int t;
int a[];
int b[];
int d[];
map<int,int> mp;
vector<int > ve[];
int n,m,p;
int main()
{
while(scanf("%d",&t)!=EOF)
{
for(int l=; l<=t; l++)
{
mp.clear();
scanf("%d %d %d",&n,&m,&p);
for(int i=; i<=n; i++)
scanf("%d",&a[i]);
int jishu=;
for(int i=; i<=m; i++ )
{
ve[i].clear();
scanf("%d",&b[i]);
if(mp[b[i]]==)
{
mp[b[i]]=jishu;
d[jishu]=i;
jishu++;
}
}
int minx=;
int what=;
for(int i=; i<=n; i++)
{
if(mp[a[i]])
{
ve[mp[a[i]]].push_back(i);
}
}
for(int i=; i<jishu; i++)
{
if(minx>ve[i].size())
{
minx=ve[i].size();
what=i;
}
}
int sum=;
for(int i=; i<ve[what].size(); i++)
{
int st=ve[what][i]-(d[mp[a[ve[what][i]]]]-)*p;
int ed=ve[what][i]+(m-d[mp[a[ve[what][i]]]])*p;
int zha=;
int flag=;
if(st<||ed>n)
flag=;
if(flag==)
{
for(int j=st; j<=ed; j+=p)
{
if(a[j]!=b[zha++])
{
flag=;
break;
}
}
}
if(flag==)
sum++;
}
printf("Case #%d: %d\n",l,sum);
}
}
return ;
}
HDU 5918 KMP/模拟的更多相关文章
- hdu 1686 KMP模板
// hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...
- Cyclic Nacklace HDU 3746 KMP 循环节
Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len ...
- 【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 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 5918 SequenceI (2016 CCPC长春站 KMP模版变形)
这个题目的数据应该是比较弱的,赛场上的时候我们暴力也过了,而且我的kmp居然比暴力还要慢-- 这个变形并不难,跳着选数,把漏掉的位置补上就可以了. 代码如下: #include<iostream ...
- hdu 5918(强行水过去..正解KMP)
Sequence I Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 2087 (KMP不可重叠的匹配) 花布条
题意: 用两个字符串分别表示布条和图案,问能从该布条上剪出多少这样的图案. 分析: 毫无疑问这也是用KMP匹配,关键是一次匹配完成后,模式串应该向后滑动多少. 和上一题 HDU 1686 不同,两个图 ...
- HDU 4121 Xiangqi 模拟题
Xiangqi Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4121 ...
- hdu 5071 Chat(模拟)
题目链接:hdu 5071 Chat 题目大意:模拟题. .. 注意最后说bye的时候仅仅要和讲过话的妹子说再见. 解题思路:用一个map记录每一个等级的妹子讲过多少话以及是否有这个等级的妹子.数组A ...
随机推荐
- 二分搜索法(转载自vanezkw)
二分查找算法java实现 今天看了一下JDK里面的二分法是实现,觉得有点小问题.二分法的实现有多种今天就给大家分享两种.一种是递归方式的,一种是非递归方式的.先来看看一些基础的东西. 1.算法概念. ...
- treap 1296 营业额统计
有一个点答案错误,求大神指教 #include<cstdio>#include<iostream>#include<cstdlib>#include<ctim ...
- 火狐和IE的window.event对象详解(转载)
FF的FIREBUG,不仅能测试JS还能检查CSS错误,是一般常用的. 但它主要检查FF方面的错误,对IE就无能为力了. 要测试IE,就用ieTester,它可以测试IE几乎所有版本(1.0恐怕也用不 ...
- 经典线程同步 事件Event
阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇 一个经典的多线程同步问题> <秒杀多线程第五篇 经典线程同步关键段CS> 上一篇中使用关键段来解决经典的多线程同步互斥问题 ...
- 使用icon替换你的网页图标(转)
第一次使用 Font Awesome 发现相当的爽呀!它的图标很全,能够帮你节约时间去找图片.下面就来一起学习吧: 1: 去官方网站下载解压 http://fontawesome.io/ 2: 解压后 ...
- MapReduce数据流(一)
在上一篇文章中我们讲解了一个基本的MapReduce作业由那些基本组件组成,从高层来看,所有的组件在一起工作时如下图所示: 图4.4高层MapReduce工作流水线 MapReduce的输入一般来自H ...
- 理解Mach Port
参考文档: 1. http://robert.sesek.com/thoughts/2012/1/debugging_mach_ports.html 2. Mach 3 Kernel Interfac ...
- 将salt取到的数据处理
#!/usr/bin/env python #coding:utf-8 import json with open('minfo') as f,open('minfoMiddle','w') as f ...
- java equals 和hashcode
1 如果不知道怎么重载hashcode, eclipse自动代码生成工具会帮助你生成,大概的思路是设定一个int prim, 然后根据各个成员的值或者hashcode值进行某种运算即可,具体什么运 ...
- Mifare Classic Tool(MCT)汉化版
2.0.4 到 2.0.6的更改: 注意:本汉化版本可能不稳定,与此发生的一切后果与作者和汉化者无关. Version : * Bugfix: Fixed crash which occurred i ...