Sequence I

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

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
 
题意:给定数组 a1...an ,b1...bm, 以及间隔 d,问 a 中使得存在 aq ,aq+d , aq+2d...aq+(m-1)d  和 b数组相等的 q 有多少个?
题解:标准解法是 KMP,我这双重循环强行水了过去..
#include <bits/stdc++.h>
using namespace std;
const int N = ;
int a[N],b[N];
int main()
{
int tcase,t=;
scanf("%d",&tcase);
while(tcase--){
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=;i<=m;i++){
scanf("%d",&b[i]);
}
if(m>n) {
printf("Case #%d: %d\n",t++,);
continue;
}
int ans = ;
for(int i=;i<=n;i++){
if(a[i]!=b[]) continue;
int idx = ;
for(int j=i;j<=n;j+=k){
if(a[j]==b[idx]){
idx++;
}
else break;
if(idx==m+){
ans++;break;
}
}
}
printf("Case #%d: %d\n",t++,ans);
}
return ;
}
 KMP解法
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std; const int N = ;
int Next[N];
int A[N],S[N], T[N];
int slen, tlen; void getNext()
{
int j, k;
j = ; k = -; Next[] = -;
while(j < tlen)
if(k == - || T[j] == T[k])
Next[++j] = ++k;
else
k = Next[k]; }
/*
返回模式串在主串S中出现的次数
*/
int KMP_Count()
{
int ans = ;
int i, j = ;
if(slen == && tlen == )
{
if(S[] == T[])
return ;
else
return ;
}
for(i = ; i < slen; i++)
{
while(j > && S[i] != T[j])
j = Next[j];
if(S[i] == T[j])
j++;
if(j == tlen)
{
ans++;
j = Next[j];
}
}
return ans;
}
int main()
{ int tcase,t=;
scanf("%d",&tcase);
while(tcase--)
{
int n,k;
scanf("%d%d%d",&n,&tlen,&k); memset(T,,sizeof(T));
for(int i=;i<n;i++) scanf("%d",&A[i]);
for(int i=;i<tlen;i++) scanf("%d",&T[i]);
int ans = ;
getNext();
for(int i=;i<k;i++){ ///枚举起点
slen = ;
for(int j = i;i+(tlen-)*k<n&&j<n;j+=k){
S[slen++] = A[j];
}
if(slen<tlen) continue;
/*for(int j=0;j<slen;j++){
printf("%d ",S[j]);
}*/
ans+=KMP_Count();
}
printf("Case #%d: %d\n",t++,ans);
}
return ;
}

hdu 5918(强行水过去..正解KMP)的更多相关文章

  1. POJ 2185 正解 KMP

    题意: 思路: 把每一行压成一个数 求一下 KMP 把每一列压成一个数 求一下KMP 答案就是两个周期之积 网上的好多题解都是错的---------.. //By SiriusRen #include ...

  2. HDU 4691 正解后缀数组(暴力也能过)

    本来是个后缀数组,考察算法的中级题目,暴力居然也可以水过,就看你跳不跳坑了(c++和G++返回结果就很不一样,关键看编译器) 丝毫不差的代码,就看运气如何了.唯一差别c++还是G++,但正解是后缀数组 ...

  3. HDU 4251 --- 主席树(划分树是正解)

    题意:查询区间中位数 思路:模板题,相当于区间第K大的数,主席树可以水过,但划分树是正解.但还没搞明白划分树,先上模板 #include <iostream> #include <c ...

  4. HDU 6625 three arrays 求两个序列异或最小值的排列(一个可以推广的正解

    目录 题意: 解析 原题描述 字典树动态求Mex @(hdu 6625求两个序列异或最小值的排列) 题意: \(T(100)\)组,每组两个长度为\(n(100000)\)的排列,你可以将\(a[]\ ...

  5. poj1015 正解--二维DP(完全背包)

    题目链接:http://poj.org/problem?id=1015 错误解法: 网上很多解法是错误的,用dp[i][j]表示选择i个人差值为j的最优解,用path[i][j]存储路径,循环次序为“ ...

  6. 【BZOJ-4059】Non-boring sequences 线段树 + 扫描线 (正解暴力)

    4059: [Cerc2012]Non-boring sequences Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 440  Solved: 16 ...

  7. Linux 下 netbeans 字体抗锯齿正解

    转自:http://leenjewel.blog.163.com/blog/static/601937922010124444051/ 说来这个不难,主要是我看网上有的写的不是很明确,甚至有的写的根本 ...

  8. Android事件模型之interceptTouchEvnet ,onTouchEvent关系正解

    首先,看Android的官方文档正解 onInterceptTouchEvent()与onTouchEvent()的机制: 1. down事件首先会传递到onInterceptTouchEvent() ...

  9. 分享网上搜到的Oracle中对判定条件where 1=1的正解

    今天在网上找到了Oracle中对判定条件where 1=1的正解,粘贴出来和大家分享下 1=1 是永恒成立的,意思无条件的,也就是说在SQL语句里有没有这个1=1都可以. 这个1=1常用于应用程序根据 ...

随机推荐

  1. Function.caller, arguments.caller, arguments.callee, arguments.callee.calller

    Function.caller指向当前函数的调用者,是arguments.caller的替代者 arguments.caller也是指向当前函数的调用者,已被废弃 arguments.callee是对 ...

  2. 解题:SDOI 2013 保护出题人

    题面 首先是愉快的推式子 $dp[i]=max(dp[i],\frac{sum[i]-sum[j-1]}{x[i]+(i-j)*d})(1<=j<=i<=n)$(考虑有一只僵尸正好走 ...

  3. dorado7常用内容

    1.dataset添加数据this.get("#dsProduct").getData().insert();或者this.get("#dsProduct"). ...

  4. 关于使用EmguCV出现 “无法加载 DLL“cvextern”: 找不到指定的程序” 的解决方法

    http://blog.csdn.net/cdjcong/article/details/8444191 查找了网上的一些说法,都是说没有设置好路径,或者未将DLL文件复制到Debug文件夹下,但是我 ...

  5. 「Python-Django」django 实现将本地图片存入数据库,并能显示在web上

    1. 将图片存入数据库 关于数据库基本操作的学习,请参见这一篇博客:https://www.cnblogs.com/leejy/p/6745186.html 这里我默认,您已经会了基本操作,能在数据库 ...

  6. centos pure-ftpd配置及错误解决

    使用yum安装pure-ftpd Pure-FTPd是Linux上的一个开源的FTP服务程序,在易用性.配置性上比vsftp较方便,下面我们使用centos6演示安装和配置pure-ftpd. 安装e ...

  7. SpringCloud学习(2)——Rest微服务案例

    创建父工程: microservicecloud  创建公共模块api:microservicecloudapi SQL脚本: 此学习路线总共创建3个库, 分别为clouddb01, clouddb0 ...

  8. echart图跟随屏幕自适应变化

    var myChart = echarts.init(document.getElementById('main'),'macarons');// var option = { //...一些配置 / ...

  9. [数据库中间件]centos6.6下配置libzdb所产生的错误

    1.关于gmtime_r.timegm的隐藏声明错误,从系统的time.h中复制两个函数引用到libzdb自己定义的time.h,代码如下: extern struct tm *gmtime_r (c ...

  10. 关于Java泛型深入理解小总结

    1.何为泛型 首先泛型的本质便是类型参数化,通俗的说就是用一个变量来表示类型,这个类型可以是String,Integer等等不确定,表明可接受的类型,原理类似如下代码 int pattern; //声 ...