Sequence I (hdu 5918)

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1938    Accepted Submission(s): 730

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

//题意: 字符串匹配,就是,n 长主串,m 长匹配串,k 长间隔,问有多少种匹配?

分成 k 组就好,这题可以用来测测你的 KMP 模板哦,数据还可以,就是,就算是朴素匹配也能过。。。

做完我算是真的理解KMP了,对于字符串,有个 \0 结尾的特性,所以优化的 next 是可行的,但是这种却不行,而且,优化的并不好求匹配数。

KMP 模板 : http://www.cnblogs.com/haoabcd2010/p/6722073.html

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <vector>
  5. using namespace std;
  6. #define MX 1000005
  7.  
  8. int n,m,p;
  9. int ans;
  10. int t[MX];
  11. vector<int> zu[MX];
  12. int net[MX];
  13.  
  14. void Init()
  15. {
  16. for (int i=;i<=n;i++)
  17. zu[i].clear();
  18. }
  19.  
  20. void get_next()
  21. {
  22. int i=,j=-;
  23. net[]=-;
  24. while (i<m)
  25. {
  26. if (j==-||t[i]==t[j]) net[++i]=++j;
  27. else j = net[j];
  28. }
  29.  
  30. for (int i=;i<=m;i++)
  31. printf("%d ",net[i]);
  32. printf("\n");
  33. }
  34.  
  35. void KMP(int x)
  36. {
  37. int i=,j=;
  38. int len = zu[x].size();
  39. while(i<len&&j<m)
  40. {
  41. if (j==-||zu[x][i]==t[j])
  42. {
  43. i++,j++;
  44. }
  45. else j=net[j];
  46. if (j==m)
  47. {
  48. ans++;
  49. j = net[j];
  50. }
  51. }
  52. }
  53.  
  54. int main()
  55. {
  56. int T;
  57. scanf("%d",&T);
  58. for(int cas=;cas<=T;cas++)
  59. {
  60. scanf("%d%d%d",&n,&m,&p);
  61. Init();
  62. for (int i=;i<n;i++)
  63. {
  64. int x;
  65. scanf("%d",&x);
  66. zu[i%p].push_back(x);
  67. }
  68. for (int i=;i<m;i++)
  69. scanf("%d",&t[i]);
  70. get_next();
  71.  
  72. ans = ;
  73. for (int i=;i<p;i++) KMP(i);
  74. printf("Case #%d: %d\n",cas,ans);
  75. }
  76. return ;
  77. }

Sequence I的更多相关文章

  1. oracle SEQUENCE 创建, 修改,删除

    oracle创建序列化: CREATE SEQUENCE seq_itv_collection            INCREMENT BY 1  -- 每次加几个              STA ...

  2. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  3. DG gap sequence修复一例

    环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...

  4. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  6. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  7. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  8. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  10. Leetcode 60. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

随机推荐

  1. [iOS]在NavigationController中的ScrollView中的子视图都会下移64个像素

    情况是这种: 我有一个UINavigationController,设置为self.window的root视图, 然后有一个UIVIewController是UINavigtionController ...

  2. 微信团队原创分享:iOS版微信的内存监控系统技术实践

    本文来自微信开发团队yangyang的技术分享. 一.前言 FOOM(Foreground Out Of Memory),是指App在前台因消耗内存过多引起系统强杀.对用户而言,表现跟crash一样. ...

  3. SpringBoot学习小结

    基于Spring,简化Spring应用开发的框架,整个Spring技术栈的大整合,J2EE开发的一站式解决方案 优点: 快速创建独立运行的Spring项目以及集成主流框架 使用嵌入式的Servlet容 ...

  4. linux文本命令

    1.find和grep find命令的作用是在目录中根据文件名搜索文件,grep命令的作用是在目录中根据文件内容搜索文件,find和grep的使用权限是所有用户. (1)find命令: find 列出 ...

  5. Android应用中使用百度地图API之POI(三)

    先看执行后的图吧: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWFqaWFuamll/font/5a6L5L2T/fontsize/400/fill/ ...

  6. 【Linux指标】内存篇

    1:内存使用率 指标名称 指标含义 单位 采集方式(Linux) 采集方式(Windows) AGT.可用内存 GB 通过/proc/meminfo得到MemAvailable;若/proc/memi ...

  7. [原创]JAVA号码工具类:实现手机固话号码判断与区号截取

    工具类说明 该工具类主要是用于判断号码的类型,如果是手机类型,则返回号码前7位,便于后面继续判断号码归属地:如果是固话类型,则截取固话的区号,同样也是为了后面判断号码的归属地. 在获取到这些信息之后, ...

  8. Atitit.互联网 软件编程 数据库方面 架构 大牛 牛人 attilax总结

    Atitit.互联网 软件编程 数据库方面 架构 大牛 牛人 attilax总结 Coolshell 称号.理论与c++ 阮一峰:: 理论高手与js高手 王银:理论高手 赵劼,网名老赵,c#高手 与理 ...

  9. Angularjs学习笔记8_directive2

    指令难点在于参数 angular.module('app', []) .directive('myDirective', function() { return { restrict: String, ...

  10. Tomcat闲聊

    上面是Tomcat的执行过程. 下面说几个概念: 1.JSP:这个就是在html文件中嵌入Java代码之后重新生成的一个文件(.jsp的文件) 2.servlet:就是一个处理动态资源的小程序,一般位 ...