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 ...
随机推荐
- vue - 总结build.js
1. 导入外部包,用关键字 const :ES2015->不可变量 内部使用变量,let -> 块级声明 2.无分号结尾(我猜大概是用了es6变量,避免了es5应缺少分号出现的一些问题) ...
- B5:责任链模式 Chain Of Responsibility
使多个对象都有机会处理处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这个对象连成一条链,并沿着该链处理请求,直到有一个对象能够处理它为止. 相当于switch/case,在客户端指定了每一链 ...
- C语言--矩阵置换
//takePlace里的循环写错了,j循环应该是 //for (j=i;j<3;j++) //你那个写的交换了2遍,又变回原来的了.*// #include <stdio.h> ] ...
- Vue webpack配置文件
一.代码地址 github:https://github.com/MengFangui/VueWebpackConfig 二.配置说明 1.命令 (1)npm i 安装依赖包 (2)num run d ...
- libevent2源码分析之三:信号的初始化流程
libevent2对信号的响应也进行了封装,使之与socket操作一样对外提供统一的接口.这里的信号一般指linux的信号.由于信号与socket相关的编程接口有较大的不同,因此在内部实现也有一些区别 ...
- 一站式学习Wireshark(转载)
一站式学习Wireshark(一):Wireshark基本用法 2014/06/10 · IT技术 · 4 评论 · WireShark 分享到: 115 与<YII框架>不得不说的故事— ...
- /proc/meminfo详解 = /nmon analysis --MEM
memtotal hightotal lowtotal swaptotal memfree highfree lowfree swapfree memshared cached active bigf ...
- Matlab、R向量与矩阵操作
Matlab.R向量与矩阵操作 描 述 Matlab R 1 建立行向量v=[1 2 3 4] v=[1 2 3 4] v<-c(1,2,3,4)或v<-scan(),然后输入 ...
- C# socket编程 使用udp实现单对单的连接对话
ipLocalPoint = new IPEndPoint(IPAddress.Parse("192.168.31.122"), 5000); //定义网络类型,数据连接类型和网络 ...
- SpringCloud系列十一:自定义Feign
1. 回顾 上文我们讲解了如何为服务消费者配置Feign. 在Spring Cloud中,Feign的默认配置类是FeignClientsConfiguration,该类定义了Feign默认使用的编码 ...