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

最新文章

  1. ASCII码而已
  2. Win7下完全卸载Oracle 11g
  3. wordpress搬家换域名
  4. Linux tr命令
  5. OC 入门
  6. linux笔记:shell基础和bash的基本功能
  7. 解决Eclipse Pydev中import时报错:Unresolved import
  8. CoreLocation框架的使用
  9. sql 判断表是否存在
  10. yii2源码学习笔记(六)
  11. (摘)Zebra打印机异常处理
  12. 【C语言探索之旅】 第二部分第八课:动态分配
  13. Windows10中“SQL Server 配置管理器”哪去了?
  14. Ubuntu命令用法详解——curl命令
  15. python爬虫数据解析之xpath
  16. JS学习笔记Day24
  17. 新手入门HTML5有什么推荐的书籍?
  18. django rest framework 项目创建
  19. python之路--基础数据类型的补充与深浅copy
  20. 【CF618F】Double Knapsack(构造)

热门文章

  1. hadoop MapReduce Yarn运行机制
  2. sizeof进行结构体大小的判断
  3. spinlock原理
  4. rsa加密解密
  5. 1.JS设计模式-this,call&amp;apply
  6. 5. javacript高级程序设计-引用类型
  7. ABAP 内表的行列转换
  8. 【leetcode】 Interleaving String (hard)
  9. 【编程题目】和为 n 连续正数序列
  10. 【XLL 文档翻译】【第3部分】必要的和有用的 C API XLM 函数