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. c++常量详解

    概念 常量是存放固定且不可变值的,一旦确定初始值则在程序其它地方不可改变, 所以const对象必须初始化.常量一般使用const关键字来修饰. const 对象可以大致分为三类: 1. const i ...

  2. shell函数使用

    函数定义格式: 和js有点类似,不过在 shell 中 function 关键字是可选的. 如: sum { // shell 语句 } function sum() { // shell 语句 } ...

  3. windows使用composer.phar

    把composer.phar复制到php安装目录下, 新建一个composer.bat文件,编辑 @php "%~dp0composer.phar" %* 保存,然后就可以使用co ...

  4. 把一个文件中所有文件名或者文件路径读取到一个txt文件,然后在matlab中读取

    链接: http://blog.csdn.net/dreamgchuan/article/details/51113295 dir /on/b/s  这个读取的是这样的格式:

  5. duilib 修复CTreeViewUI复选功能判断不准确的bug

    转载请说明出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/42265209 CTreeViewUI里面自带了复选的功能,但是复选功能存在 ...

  6. SQL SERVER2008不在同一局域网内实现订阅发布的方法

    我们知道,在新建订阅的时候,必须要使用实际服务器的真实名称才可以,IP地址,别名都不可以,在同一个局域网内是没有问题的.但是两台服务器不在同一个局域网内就不行了 要想实现订阅和发布,本人用到的最简单的 ...

  7. 【BZOJ】2154: Crash的数字表格 莫比乌斯反演

    [题意]给定n,m,求Σlcm(i,j),1<=i<=n,1<=j<=m,n,m<=10^7. [算法]数论(莫比乌斯反演) [题解] $$ans=\sum_{i\leq ...

  8. react 项目遇到的警告集锦

    1.  2.

  9. DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead extract-text-webpack-plugin 提取css报错

    深入浅出Webpack 1-5 使用pulugin extract-text-webpack-plugin 提取css报错 DeprecationWarning: Tapable.plugin is ...

  10. JS 检测客户端断网情况

    常用方法 1 navigator.onLine 2 window.addEventListener() 3 获取网络资源 4 ajax请求 1. navigator.onLine 只会在机器未连上路由 ...