题意:

用第二个数列去匹配第一个数列,输出第一次匹配到的位置,如果没有则输出-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的更多相关文章

  1. HDU 1711(KMP)字符串匹配

    链接  HDU 1711 Number Sequence KMP 算法 我以自己理解写的,写的不对,不明白的地方海王子出来,一起共同学习: 字符串匹配 就是KMP,一般思想,用一个for循环找开头   ...

  2. Number Sequence HDU 1711(KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 首次接触KMP,自己都不是特别理解.在网上百度看了好几个帖子之后,对KMP也有了初步的理解. #inclu ...

  3. Number Sequence - HDU 1711(KMP模板题)

    题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1   分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...

  4. HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. 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 ...

  6. (KMP 模板)Number Sequence -- Hdu -- 1711

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Other ...

  7. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

  8. HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...

  9. HDU 1711 Number Sequence(KMP)附带KMP的详解

    题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...

随机推荐

  1. 交互式shell和非交互式shell、登录shell和非登录shell的区别

    交互式shell和非交互式shell.登录shell和非登录shell的区别.首先,这是两个不同的维度来划分的,一个是是否交互式,另一个是是否登录. 交互式shell和非交互式shell(intera ...

  2. WPF 多线程处理(5)

    WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 项目的目录: 以下是FileStroage的 ...

  3. ssh-add 报错 Could not open a connection to your authentication agent

    ERROR: [root@testcentos01 ~]# ssh-add Could not open a connection to your authentication agent 在shel ...

  4. PAT-乙级-1049. 数列的片段和(20)

    1049. 数列的片段和(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 给定一个正数数列,我们可以从中截 ...

  5. uva 534

    floyd算法 数据量比较小  就简单了~ /************************************************************************* > ...

  6. POJ 3258

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5961   Accepted: 2579 D ...

  7. ios 沙盒 NSCoding(相当于JAVA对象序列化) 归档 数据存储

    通过NSCoding能实现像JAVA一样能够实现对象的序列化,可以保存对象到文件里. NSCoding 跟其他存储方式略有不同,他可以存储对象 对象存储的条件是: 对象需要遵守 NSCoding 协议 ...

  8. POJ1008Maya Calendar

    http://poj.org/problem?id=1008&lang=default&change=true 这个题倒是不难,就是麻烦一点,但是还WA了几次都是因为处理天数的时候没处 ...

  9. lintcode:落单的数

    题目: 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 样例 给出 [1,2,2,1,3,4,3],返回 4 挑战 一次遍历,常数级的额外空间复杂度 ...

  10. jdbc的通讯录CRUD

    基于JDBC的通讯录练手:项目以MVC模式开发,包名:cn.itcast.txl.domain;cn.itcast.txl.dao;cn.itcast.txl.dao.impl;cn.itcast.t ...