Sequence I
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
1
1
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
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- #include <vector>
- using namespace std;
- #define MX 1000005
- int n,m,p;
- int ans;
- int t[MX];
- vector<int> zu[MX];
- int net[MX];
- void Init()
- {
- for (int i=;i<=n;i++)
- zu[i].clear();
- }
- void get_next()
- {
- int i=,j=-;
- net[]=-;
- while (i<m)
- {
- if (j==-||t[i]==t[j]) net[++i]=++j;
- else j = net[j];
- }
- for (int i=;i<=m;i++)
- printf("%d ",net[i]);
- printf("\n");
- }
- void KMP(int x)
- {
- int i=,j=;
- int len = zu[x].size();
- while(i<len&&j<m)
- {
- if (j==-||zu[x][i]==t[j])
- {
- i++,j++;
- }
- else j=net[j];
- if (j==m)
- {
- ans++;
- j = net[j];
- }
- }
- }
- int main()
- {
- int T;
- scanf("%d",&T);
- for(int cas=;cas<=T;cas++)
- {
- scanf("%d%d%d",&n,&m,&p);
- Init();
- for (int i=;i<n;i++)
- {
- int x;
- scanf("%d",&x);
- zu[i%p].push_back(x);
- }
- for (int i=;i<m;i++)
- scanf("%d",&t[i]);
- get_next();
- ans = ;
- for (int i=;i<p;i++) KMP(i);
- printf("Case #%d: %d\n",cas,ans);
- }
- return ;
- }
Sequence I的更多相关文章
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- DG gap sequence修复一例
环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [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 ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 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 ...
随机推荐
- [iOS]在NavigationController中的ScrollView中的子视图都会下移64个像素
情况是这种: 我有一个UINavigationController,设置为self.window的root视图, 然后有一个UIVIewController是UINavigtionController ...
- 微信团队原创分享:iOS版微信的内存监控系统技术实践
本文来自微信开发团队yangyang的技术分享. 一.前言 FOOM(Foreground Out Of Memory),是指App在前台因消耗内存过多引起系统强杀.对用户而言,表现跟crash一样. ...
- SpringBoot学习小结
基于Spring,简化Spring应用开发的框架,整个Spring技术栈的大整合,J2EE开发的一站式解决方案 优点: 快速创建独立运行的Spring项目以及集成主流框架 使用嵌入式的Servlet容 ...
- linux文本命令
1.find和grep find命令的作用是在目录中根据文件名搜索文件,grep命令的作用是在目录中根据文件内容搜索文件,find和grep的使用权限是所有用户. (1)find命令: find 列出 ...
- Android应用中使用百度地图API之POI(三)
先看执行后的图吧: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWFqaWFuamll/font/5a6L5L2T/fontsize/400/fill/ ...
- 【Linux指标】内存篇
1:内存使用率 指标名称 指标含义 单位 采集方式(Linux) 采集方式(Windows) AGT.可用内存 GB 通过/proc/meminfo得到MemAvailable;若/proc/memi ...
- [原创]JAVA号码工具类:实现手机固话号码判断与区号截取
工具类说明 该工具类主要是用于判断号码的类型,如果是手机类型,则返回号码前7位,便于后面继续判断号码归属地:如果是固话类型,则截取固话的区号,同样也是为了后面判断号码的归属地. 在获取到这些信息之后, ...
- Atitit.互联网 软件编程 数据库方面 架构 大牛 牛人 attilax总结
Atitit.互联网 软件编程 数据库方面 架构 大牛 牛人 attilax总结 Coolshell 称号.理论与c++ 阮一峰:: 理论高手与js高手 王银:理论高手 赵劼,网名老赵,c#高手 与理 ...
- Angularjs学习笔记8_directive2
指令难点在于参数 angular.module('app', []) .directive('myDirective', function() { return { restrict: String, ...
- Tomcat闲聊
上面是Tomcat的执行过程. 下面说几个概念: 1.JSP:这个就是在html文件中嵌入Java代码之后重新生成的一个文件(.jsp的文件) 2.servlet:就是一个处理动态资源的小程序,一般位 ...