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 ...
随机推荐
- selenuim-webdriver注解之@FindBy、@FindBys、@FindAll的区别
selenium-webdriver中获取页面元素的方式有很多,使用注解获取页面元素是其中一种途径, 方式有3种:@FindBy.@FindBys.@FindAll.下文对3中类型的区别和使用场景进行 ...
- atitit.MyEclipse10 中添加svn插件故障排除
atitit.MyEclipse10 中添加svn插件故障排除 删除\configuration \org.eclipse.update 不行... 二. 在configuration下的config ...
- Bootstrap-初步学习
<!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8&quo ...
- Python 解码 Unicode 转义字符串 (转)
其实,这里要讨论的内容是针对 Python2 的,实际上也是 Python2 中让人头疼的编码问题,而 Python3 则好处理得多. 先来看看例子: >>> s = "我 ...
- C#中ArrayList类的使用
ArrayList类 使用大小可按须要动态添加的数组实现IList接口 命名空间:System.Collections 程序集:mscorlib 语法: public class ArrayList: ...
- web 图片上传实现本地预览
在说上传之前先说说如何替换or美化浏览器自带的简陋上传按钮(自定义自己的上传按钮 如:img): 1.将自定义上传按钮上方添加 input file 框,实现input实现透明处理. 2.对自定义上传 ...
- NIO之缓冲区(Buffer)的数据存取
缓冲区(Buffer) 一个用于特定基本数据类行的容器.有java.nio包定义的,所有缓冲区都是抽象类Buffer的子类. Java NIO中的Buffer主要用于与NIO通道进行交互,数据是从通道 ...
- unity3d 导入google play services插件工程
最近在给unity工程尝试接入google play services插件,遇到了些问题,记录一下. 之前在做android插件的时候,都是自己创建一个android工程,把生成的.class文件打包 ...
- 使用Charles对Https请求进行抓包
昨天对某个APP做分析的时候发现其请求是Https的,抓包工具不能正常的显示请求的内容及返回的情况.通过搜索发现Charles是支持针对Https抓包的.具体的操作如下: 1.电脑端安装SSL证书 2 ...
- DataUml Design 介绍10 - DataUML 1.4版本功能(增加UML活动图和用例图)
DataUml Design 1.4版本正式发布 1.解决软件自动关闭的问题 2.增加UML活动图和用例图功能 详细介绍稍后继续...........................