Sequence I

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,⋯,bmis 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
 

题意:

  给你a,b两个序列

  和一个p ,求有多少个 q恰好满足 b1,b2,b3....bm 就是 a[q],a[q+p],a[q+2p]......a[q+(m-1)p];

题解:

  将a序列,每隔p位置分成一组,这样最多有p组,个数和是n

  将每组和b序列跑KMP计算答案

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 1e6+, M = 1e6, mod = 1e9+, inf = 2e9; int T,n,m,p,s[N],t[N],ans = ;
vector<int > P[N];
int nex[N];
int main()
{
int cas = ;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&p);
for(int i = ; i <= n; ++i) scanf("%d",&t[i]);
for(int i = ; i <= m; ++i) scanf("%d",&s[i]);
for(int i = ; i < p; ++i) P[i].clear();
memset(nex,,sizeof(nex));
ans = ;
int k = ;
for(int i=; i<=m; i++)
{
while(k>&&s[k+]!=s[i]) k = nex[k];
if(s[k+]==s[i])k++;
nex[i] = k;
}
if(p == )
{
k = ;
for(int i=; i<=n; i++)
{
while(k>&&s[k+]!=t[i]) k = nex[k];
if(s[k+]==t[i]) k++;
if(k==m) {
k = nex[k];
ans++;
}
}
printf("Case #%d: ",cas++);
printf("%d\n",ans);
}
else {
for(int i = ; i <= n; ++i)
P[i % p].push_back(t[i]);
for(int i = ; i < p; ++i) {
k = ;
for(int j = ; j < P[i].size(); ++j) {
while(k>&&s[k+]!=P[i][j]) k = nex[k];
if(s[k+]==P[i][j]) k++;
if(k==m) {
k = nex[k];
ans++;
}
}
}
printf("Case #%d: ",cas++);
printf("%d\n",ans); }
}
}

HDU 5918 Sequence I KMP的更多相关文章

  1. HDU 1711Number Sequence【KMP模板题】

    <题目链接> 题目大意: 意思是给出两个串,找出匹配串在模式串中的位置. 解题分析: KMP算法模板题. #include <cstdio> #include <cstr ...

  2. HDU 5918 Sequence I

    题目来源:2016 CCPC 长春站 题意:给出两个序列 a[] , b[] ,如果b1,b2....bm能够与aq,aq+p,aq+2p...aq+(m-1)p对应( q+(m-1)p<=n ...

  3. HDU 3397 Sequence operation(线段树)

    HDU 3397 Sequence operation 题目链接 题意:给定一个01序列,有5种操作 0 a b [a.b]区间置为0 1 a b [a,b]区间置为1 2 a b [a,b]区间0变 ...

  4. hdu 5510 Bazinga(字符串kmp)

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  5. 【hdu 5918】Sequence I(KMP)

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

  6. HDU 1711 Number Sequence (KMP简单题)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...

  8. HDU - 1711 A - Number Sequence(kmp

    HDU - 1711 A - Number Sequence   Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1 ...

  9. HDU 1711 Number Sequence(KMP)附带KMP的详解

    题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...

随机推荐

  1. C++ 获取vector容器最后一个元素

    声明:vector<T>  vec; 方法一: return vec.at(vec.size()-1); 方法二: return vec.back(); 方法三: return vec.e ...

  2. android 和iOS的view上的区别

    android上的view的类叫View, 以下是它的class overview, This class represents the basic building block for user i ...

  3. js中修改标签的hidden属性

    hidden属性在html5中,只要存在,就是隐藏效果,而不论值为多少 要显示元素,要删除hidden属性,而不是设置为false <script type="text/javascr ...

  4. C#操作字符串方法总结<转>

    staticvoid Main(string[] args) { string s =""; //(1)字符访问(下标访问s[i]) s ="ABCD"; Co ...

  5. [Android]drawable-nodpi文件夹 的作用

    把一些不能被拉伸的图片放在 drawable-nodpi 中,此图片将不会被放大,以原大小显示. 看下图: 这两个图片被放到了drawable-nodpi 中 在不同分辨率下的显示大小是一样的,不会被 ...

  6. Match:Milking Grid(二维KMP算法)(POJ 2185)

    奶牛矩阵 题目大意:给定一个矩阵,要你找到一个最小的矩阵,这个矩阵的无限扩充的矩阵包含着原来的矩阵 思路:乍一看这一题确实很那做,因为我们不知道最小矩阵的位置,但是仔细一想,如果我们能把矩阵都放在左上 ...

  7. hdu 1972.Printer Queue 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1972 题目意思:需要模拟打印机打印.打印机里面有一些 job,每个job被赋予1-9的其中一个值,越大 ...

  8. 【leetcode】Sum Root to Leaf Numbers(hard)

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  9. 【linux】ubuntu stmp服务器配置

    来源:http://blog.itpub.net/786540/viewspace-1002077/ sudo apt-get install sendmail(其中已经包含了sendmail-bin ...

  10. 20145213《Java程序设计》第九周学习总结

    20145213<Java程序设计>第九周学习总结 教材学习总结 "五一"假期过得太快,就像龙卷风.没有一点点防备,就与Java博客撞个满怀.在这个普天同庆的节日里,根 ...