HDU 1711 Number Sequence (数字KMP,变形)
题意:
在一个序列中找到一个连续的子序列,返回其开始位置。
思路:
每个数字当成1个字符,长的序列是原串,短的序列是模式串,求next数组后进行匹配。
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
const int N=;
int a[N];
int nextt[];
int b[];
int n, m; void get_next()
{
nextt[]=-;
int i=, j=-;
while(i<m)
{
if(j==- || b[i]==b[j])
{
nextt[i+]=++j;
++i;
}
else
j=nextt[j];
}
} int cal()
{ get_next();
/*
for(int i=0; i<=m; i++)
cout<<next[i]<<" ";
cout<<endl;*/
if(m>n) return -;
int i=, j=, cnt=;
while(i<n)
{
if(j==-||a[i]==b[j])
i++,j++;
else
j=nextt[j];
if(j==m) return i-m+;
}
return -;
} int main()
{
//freopen("input.txt", "r", stdin);
int t;
cin>>t;
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=; i<n; i++)
scanf("%d",&a[i]);
for(int i=; i<m; i++)
scanf("%d",&b[i]);
printf("%d\n",cal());
} return ;
}
AC代码
HDU 1711 Number Sequence (数字KMP,变形)的更多相关文章
- HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...
- HDU 1711 Number Sequence(KMP)附带KMP的详解
题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...
- HDU 1711 Number Sequence (KMP简单题)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 1711 Number Sequence(KMP匹配数字串)
这个就是kmp的数组形式,不用来处理字符串还真有点不习惯呢... #include<iostream> using namespace std; ,MAXM = ; int T[MAXN] ...
- HDU 1711 Number Sequence(kmp)
Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...
- HDU 1711 Number Sequence(KMP模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. #include<iostream> #include<cs ...
- HDU 1711 Number Sequence (KMP)
白书说这个是MP,没有对f 数组优化过,所以说KMP有点不准确 #include <stdio.h> int a,b; int T[1000010],P[10010];//从0开始存 in ...
- hdu 1711 Number Sequence(KMP模板题)
我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...
- HDU 1711 Number Sequence【kmp求子串首次出现的位置】
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= ...
随机推荐
- SQL 递归查询
WITH B (FATHER,SON,ID,ALLINFO) AS (SELECT RTRIM(LTRIM(CHAR(A.ID)))|| CHAR(ROW_NUMBER() OVER(PARTITIO ...
- PHP5.4最新特性
PHP5.4最新特性 官网:ChangeLog-5.php#5.4.0 原文Oracle:LAMP 体系有了新的竞争,但此版本中的特性使 PHP 再次挑战极限. 稍微做了修改.: 概述总结:1. ...
- Windows 代码实现关机(直接黑屏)
整理资料的时候发现的以前的代码,本机Win7 x64 Sp1 运行直接关机,黑屏.就是利用RtlAdjustPrivilege函数提权,代码中的注释写的很详细了.用的VS2010写的,直接编译成x64 ...
- 为什么要关闭360云盘:新来的美工嫌我们logo太丑,所以就决定关闭了。这个理由怎么样
新来的美工嫌我们logo太丑,所以就决定关闭了.这个理由怎么样曾经拥有的不要忘记:不能得到的更要珍惜:属于自己的不要放弃:已经失去的留作回忆.我刚来~~~嘿嘿~~ 久经考验的,忠诚的国际宅男主义战士, ...
- Struts2笔记——通配符和动态方法调用
通配符映射 * 一个 Web应用可能有成百上千个 action 声明. 可以利用 struts提供的通配符映射机制把多个彼此相似的映射关系简化为一个映射关系 * 通配符映射规则 > 若 ...
- 源码深度解析SpringMvc请求运行机制(转)
源码深度解析SpringMvc请求运行机制 本文依赖的是springmvc4.0.5.RELEASE,通过源码深度解析了解springMvc的请求运行机制.通过源码我们可以知道从客户端发送一个URL请 ...
- 296. Best Meeting Point
题目: A group of two or more people wants to meet and minimize the total travel distance. You are give ...
- C++:构造函数的默认参数知识拓展
和普通函数一样,构造函数中参数的值既可以通过实参传递,也可以指定为某些默认值,即如果用户不指定实参值,编译系统就使形参取默认值. 例9.3的问题也可以使用包含默认参数的构造函数来处理. [例9 ...
- CentOS Mysql自动备份 .txt
http://fly.b3log.org/articles/2011/10/25/1319505473928.html MySql数据库备份: 说明:我这里要把MySql数据库存放目录/var/lib ...
- Spring Boot Servlet
上一篇我们对如何创建Controller 来响应JSON 以及如何显示数据到页面中,已经有了初步的了解. Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Serv ...