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 ;
}
 

最新文章

  1. 从github上获取资源速度慢的解决办法
  2. 设为首页 和 收藏本站js代码 兼容IE,chrome,ff
  3. windows 64位 dll文件 位置及python包rtree shapely安装
  4. H5网站借鉴
  5. JAVA使用JNI调用C++动态链接库
  6. emoji处理方法汇总
  7. 编程解读 + DOS搞笑开机GIF
  8. php socket 通信
  9. TCP/IP协议族-----10、搬家IP
  10. hdu1003 Max Sum(经典dp )
  11. git add -f
  12. ubuntu文件搜索统计
  13. 关于charles抓不到js文件的问题
  14. python 装饰器的应用
  15. Python(字符串操作实例1)一个字符串用空格隔开
  16. Luogu 2668 NOIP 2015 斗地主(搜索,动态规划)
  17. python 全栈开发,Day139(websocket原理,flask之请求上下文)
  18. C#操作XML方法详解
  19. gitlab常用命令
  20. java继承2——类与继承(转)

热门文章

  1. JAVA与指针
  2. ZOJ 3654 Letty&#39;s Math Class 模拟 难度:0
  3. NOIP2005 等价表达式 解题报告
  4. UVA 10970-Big Chocolate
  5. SrcollView分页加载数据(第二种方法 自定义listView)
  6. (转)SQLLite数据操作
  7. JSP重定向传递参数
  8. SQL中让某一字段更新时自动加1
  9. 谈谈你对http的理解
  10. Java容器类概述