HDU 1711 (裸KMP) Number Sequence
题意:
用第二个数列去匹配第一个数列,输出第一次匹配到的位置,如果没有则输出-1.
分析:
这明显是一道裸的KMP。
我是在这篇博客上学的KMP算法的,讲得比较透彻。
http://blog.csdn.net/v_july_v/article/details/7041827
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn1 = + ;
const int maxn2 = + ;
int a[maxn1], b[maxn2], next[maxn2];
int n, m; void get_next()
{
next[] = -;
int k = -;
int j = ;
while(j < m - )
{
if(k == - || b[j] == b[k])
{
k++;
j++;
next[j] = k;
}
else k = next[k];
}
} int KMP()
{
int i = , j = ;
while(i < n && j < m)
{
if(j == - || a[i] == b[j])
{
i++;
j++;
}
else j = next[j];
}
if(j == m) return i - j;
return -;
} int main()
{
//freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
while(T--)
{
memset(next, , sizeof(next)); 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]);
get_next();
printf("%d\n", KMP()+);
} return ;
}
代码君
HDU 1711 (裸KMP) Number Sequence的更多相关文章
- HDU 1711(KMP)字符串匹配
链接 HDU 1711 Number Sequence KMP 算法 我以自己理解写的,写的不对,不明白的地方海王子出来,一起共同学习: 字符串匹配 就是KMP,一般思想,用一个for循环找开头 ...
- Number Sequence HDU 1711(KMP)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 首次接触KMP,自己都不是特别理解.在网上百度看了好几个帖子之后,对KMP也有了初步的理解. #inclu ...
- Number Sequence - HDU 1711(KMP模板题)
题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1 分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...
- HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU - 1711 A - Number Sequence(kmp
HDU - 1711 A - Number Sequence Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1 ...
- (KMP 模板)Number Sequence -- Hdu -- 1711
http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Other ...
- HDU 1711 Number Sequence (字符串匹配,KMP算法)
HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...
- 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/ ...
随机推荐
- git Clone SSL certificate problem: self signed certificate
自己的git服务器遇到证书是自签的,git验证后会拒绝,此时,采用如下命令临时禁用就好 git -c http.sslVerify=false clone https://domain.com/pat ...
- bzoj 3907: 网格 组合数学
3907: 网格 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 13 Solved: 7[Submit][Status][Discuss] Descr ...
- BestCoder Round #3
Task schedule http://acm.hdu.edu.cn/showproblem.php?pid=4907 #include<cstdio> #include<cstr ...
- PHP之SQL防注入代码集合(建站常用)
SQL防注入代码一 <?php if (!function_exists (quote)) { function quote($var) { if (strlen($var)) { $var=! ...
- mysql 。。。
MySQL的相关概念介绍 MySQL 为关系型数据库(Relational Database Management System), 这种所谓的"关系型"可以理解为"表格 ...
- NGUI List<EventDelegate> 小坑
NGUI 3.0 之后 采用了 一种 全新的 事件系统 List<EventDelegate> ,使用起来并不麻烦. 但是最近做项目碰到一个小问题,特此分享一下. PS NGUI3.6.4 ...
- memory leak
In computer science, a memory leak occurs when a computer program incorrectly manages memory allocat ...
- Javascrip操作json方法
var str1 = '{ "name": "cxh", "sex": "man" }'; var data=eval( ...
- VBScript 函数
Date/Time 函数 Conversion 函数 Format 函数 Math 函数 Array 函数 String 函数 其他函数 Date/Time 函数 函数 描述 CDate 把一个有效的 ...
- TopCoder SRM 633div1
250pts PeriodicJumping 题意:从起点开始,每次按找数组jump给定的长度,即jump[0], jump[1], jump[2].....jump[n-1], 向各个方向跳,跳 ...