Sequence I

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1013    Accepted Submission(s): 393

Problem Description
Mr. Frog has two sequences a1,a2,⋯,an

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

.

 
Input
The first line contains only one integer T≤100

, 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)

.

 
Output
For each test case, output one line “Case #x: y”, where x is the case number (starting from 1) and y is the number of valid q’s.
 
Sample Input
2
6 3 1
1 2 3 1 2 3
1 2 3
6 3 2
1 3 2 2 3 1
1 2 3
 
Sample Output
Case #1: 2
Case #2: 1
 
Source
 
题意:
题解:
 
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 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/模拟的更多相关文章

  1. hdu 1686 KMP模板

    // hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...

  2. Cyclic Nacklace HDU 3746 KMP 循环节

    Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len ...

  3. 【hdu 5918】Sequence I(KMP)

    给定两个数字序列,求a序列中每隔p个构成的p+1个序列中共能匹配多少个b序列. 例如1 1 2 2 3 3 每隔1个的序列有两个1 2 3 kmp,匹配时每次主串往前p个,枚举1到p为起点. 题目 # ...

  4. 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 ...

  5. HDU 5918 SequenceI (2016 CCPC长春站 KMP模版变形)

    这个题目的数据应该是比较弱的,赛场上的时候我们暴力也过了,而且我的kmp居然比暴力还要慢-- 这个变形并不难,跳着选数,把漏掉的位置补上就可以了. 代码如下: #include<iostream ...

  6. hdu 5918(强行水过去..正解KMP)

    Sequence I Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. HDU 2087 (KMP不可重叠的匹配) 花布条

    题意: 用两个字符串分别表示布条和图案,问能从该布条上剪出多少这样的图案. 分析: 毫无疑问这也是用KMP匹配,关键是一次匹配完成后,模式串应该向后滑动多少. 和上一题 HDU 1686 不同,两个图 ...

  8. HDU 4121 Xiangqi 模拟题

    Xiangqi Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4121 ...

  9. hdu 5071 Chat(模拟)

    题目链接:hdu 5071 Chat 题目大意:模拟题. .. 注意最后说bye的时候仅仅要和讲过话的妹子说再见. 解题思路:用一个map记录每一个等级的妹子讲过多少话以及是否有这个等级的妹子.数组A ...

随机推荐

  1. elasticsearch插件之一:bigdesk

    bigdesk是elasticsearch的一个集群监控工具,可以通过它来查看es集群的各种状态,如:cpu.内存使用情况,索引数据.搜索情况,http连接数等. 可用项目git地址:https:// ...

  2. partition by

    在row_number() over()中的over使用,其后需要配合order by   ROW_NUMBER () over (partition by username   order by i ...

  3. 【转发】Linux下如何查看当前支持的文件系统及各分区的文件系统类型

    Linux下查看当前内核系统支持的文件系统: 一般都在 /lib/modules/kernl-version/kernel/fs/ 目录下包含了当前内核版本支持的文件系统: ls /lib/modul ...

  4. android 定时器的使用

    1.android中通常是使用AlarmManager来定时启动一个单次或重复多次操作的.具体的说就是我们通过AlarmManager设定一个时间和注册一个intent到系统中,然后在该时间到来时,系 ...

  5. jQuery Ajax之load()方法

    jQuery对Ajax操作进行了封装,在jQuery中$.ajax()方法属于最底层的方法,第2层是load().$.get()和$.post()方法,第3层是$.getScript()和$.getJ ...

  6. public protected default private

    简单来说,如果让一个变量或者方法,只想让自己类中的访问,那么就将它们设置成private 如果你想让一个变量或者方法,本包中的类可以访问,而且子类也可访问,但是包外的缺不想让他访问.就设置成prote ...

  7. C# 3.0 扩展方法[转载]

    实践 扩展方法是C# 3.0中新加入的特性.MSDN中对扩展方法的定义是:扩展方法使您能够向现有类型"添加"方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 以下以 ...

  8. 嵌入dll到rtf文件

    嵌入文件到doc文件中, 打开word--->插入菜单---->对象--->新建---->对象类型----->Package---->弹出创建软件包界面-----& ...

  9. USB peripherals can turn against their users

    Turning USB peripherals into BadUSB USB devices are connected to – and in many cases even built into ...

  10. 算法导论----VLSI芯片测试; n个手机中过半是好的,找出哪些是好手机

    对于分治(Divide and Conquer)的题目,最重要是 1.如何将原问题分解为若干个子问题, 2.子问题中是所有的都需要求解,还是选择一部分子问题即可. 还有一点其实非常关键,但是往往会被忽 ...