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计算答案

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #pragma comment(linker, "/STACK:102400000,102400000")
  4. #define ls i<<1
  5. #define rs ls | 1
  6. #define mid ((ll+rr)>>1)
  7. #define pii pair<int,int>
  8. #define MP make_pair
  9. typedef long long LL;
  10. const long long INF = 1e18;
  11. const double Pi = acos(-1.0);
  12. const int N = 1e6+, M = 1e6, mod = 1e9+, inf = 2e9;
  13.  
  14. int T,n,m,p,s[N],t[N],ans = ;
  15. vector<int > P[N];
  16. int nex[N];
  17. int main()
  18. {
  19. int cas = ;
  20. scanf("%d",&T);
  21. while(T--)
  22. {
  23. scanf("%d%d%d",&n,&m,&p);
  24. for(int i = ; i <= n; ++i) scanf("%d",&t[i]);
  25. for(int i = ; i <= m; ++i) scanf("%d",&s[i]);
  26. for(int i = ; i < p; ++i) P[i].clear();
  27. memset(nex,,sizeof(nex));
  28. ans = ;
  29. int k = ;
  30. for(int i=; i<=m; i++)
  31. {
  32. while(k>&&s[k+]!=s[i]) k = nex[k];
  33. if(s[k+]==s[i])k++;
  34. nex[i] = k;
  35. }
  36. if(p == )
  37. {
  38. k = ;
  39. for(int i=; i<=n; i++)
  40. {
  41. while(k>&&s[k+]!=t[i]) k = nex[k];
  42. if(s[k+]==t[i]) k++;
  43. if(k==m) {
  44. k = nex[k];
  45. ans++;
  46. }
  47. }
  48. printf("Case #%d: ",cas++);
  49. printf("%d\n",ans);
  50. }
  51. else {
  52. for(int i = ; i <= n; ++i)
  53. P[i % p].push_back(t[i]);
  54. for(int i = ; i < p; ++i) {
  55. k = ;
  56. for(int j = ; j < P[i].size(); ++j) {
  57. while(k>&&s[k+]!=P[i][j]) k = nex[k];
  58. if(s[k+]==P[i][j]) k++;
  59. if(k==m) {
  60. k = nex[k];
  61. ans++;
  62. }
  63. }
  64. }
  65. printf("Case #%d: ",cas++);
  66. printf("%d\n",ans);
  67.  
  68. }
  69. }
  70. }

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. struts2 复杂参数封装

    1.1.1    Struts2中封装复杂类型的数据: 封装到List集合: 页面: 商品名称:<input type="text" name="products[ ...

  2. Appium+Robotframework实现Android应用的自动化测试-2:Windows中启动Appium和模拟器

    一.启动Appium 安装好了之后,在桌面或者菜单中找到Appium,分别双击或点击打开Appium.exe,如果一切正常,接着会出现一个Appium启动后的界面窗口,如下图所示. 1.1 Andro ...

  3. volley post非json格式数据并获取json数据

    在使用JsonObjectRequest时无法post非json格式的数据,因而采用StringRequest获取到相应的数据后再转为json格式的数据. //这里的上下文需要讨论 private s ...

  4. centos6.5 tomcat开机启动

    可参考:centos6.5 nginx开机启动 /etc/init.d/下添加tomcatd文件,内容如下: #!/bin/sh # # chkconfig: - # # Licensed to th ...

  5. zpf 获取表单等数据的用法

    2015年4月12日 12:25:35 星期日 zpf框架中获取表单数据的方法 //获得get,post,url中的数据 private function setData() { $this-> ...

  6. 配置IIS,Apache,PHP过程中遇到的一些问题

    下载了eclipse的最新版本,并且添加了PHP插件.为了支持多语言,决定采用UTF-8编码.但是在开发的过程中,发现代码的自动提示帮助信息显示的是乱码,PHP源文件及注释,均正常.在网上查了很多资料 ...

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

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

  8. iOS 没有安装对应客户端,不应显示对应的图标

    现在很多APP为了让用户更加快捷方便注册,都会使用第三方进行登录,例如QQ/微信/淘宝等.但是上线审核被拒,大致会出现以下内容: Additionally, we found that your ap ...

  9. iOS 发送请求时获取cookie

    Cookie: 记录者用户信息的保存在本地的用户数据,如果有会被自动附上 值得一提的是,在iOS中当你发送一个任意请求时,不管你愿不愿意,NSURLRequest都会自动帮你记录你所访问的URL上设置 ...

  10. IOS - 内购

    内购的五种产品类别 •非消耗品(Nonconsumable)买了就有,头衔,功能 –指的是在游戏中一次性购买并拥有永久访问权的物品或服务.非消耗品物品可以被用户再次下载,并且能够在用户的所有设备上使用 ...