HDU 1711 Number Sequence (字符串处理 KMP)
Problem Description
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , a[K + M - 1] = b[M]. If there are more than one K exist, output the smallest one.
Input
The first line of input is a number T which indicate the number of cases. Each case contains three lines. The first line is two numbers N and M (1 <= M <= 10000, 1 <= N <= 1000000). The second line contains N integers which indicate a[1], a[2], ...... , a[N]. The third line contains M integers which indicate b[1], b[2], ...... , b[M]. All integers are in the range of [-1000000, 1000000].
Output
For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.
Sample Input`
2
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 1 3
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 2 1`
Sample Output`
6
-1`
题目分析:
字符串匹配的问题,kmp算法的应用。定义主串s和字串t,定位字串t就是要在主串s中找到一个与字串t完全相等的子串,返回第一次找到的位置的下标。
KMP算法
#include <cstdio>
const int maxn = 10000 + 10, maxm = 1000000 + 10;
int n, m;
int p[maxn], t[maxm], next[maxn]; //定义模式串,文本串,next数组
void getNext() //O(m)复杂度求Next数组
{
int i = 0, j = 0, k = -1;
next[0] = -1;
while(j < m)
{
if(k == -1 || p[k] == p[j]) next[++j] = ++k;
else k = next[k];
}
}
int kmp()
{
int j = 0; //初始化模式串的前一个位置
getNext(); //生成next数组
for(int i = 0; i < n; i++) //遍历文本串
{
while(j && p[j] != t[i]) j = next[j];//持续走直到可以匹配
if(p[j] == t[i]) j++; //匹配成功继续下一个位置(j先加1 此时j的位置所对应的值在i的位置所对应的后面)
//if(j==m)说明子串已经匹配完了(子串的长度是m,m-1是子串的最后一个字符)
if(j == m) return i-m+2; //找到后返回第一个匹配的位置,因为返回的是逻辑下标(下标从1开始所以是i-m+2,不信自己在纸上画画 (看下图 ))
}
return -1; //找不到返回-1
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++) scanf("%d", t+i);
for(int i = 0; i < m; i++) scanf("%d", p+i);
printf("%d\n", kmp());
}
return 0;
}
解释为什么返回 i-m+2
HDU 1711 Number Sequence (字符串处理 KMP)的更多相关文章
- HDU 1711 Number Sequence (字符串匹配,KMP算法)
HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- 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) ...
- KMP - HDU 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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 基础题
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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 ...
随机推荐
- Vue使用,异步获取日期时间后格式成"/Date(1333245600000+0800)/" 转换成正常格式
js从后台mvc中日期获取,结果格式成"/Date(1333245600000+0800)/"了,当然不能这样展显给用户了,要转换,方法如下: function data_stri ...
- 【Python】Python网络编程
python内置封装了很多常见的网络协议的库,因此python成为了一个强大的网络编程工具,这里是对python的网络方面编程的一个简单描述. urllib 和 urllib2模块 urllib 和u ...
- ZOJ2725_Digital Deletions
题意是这样的,一开始给你一串数字,两个人轮流操作,操作可以分为两种. 1.每次修改一个数字,使其变为一个小于当前的非负数. 2.移除中间的某一个0以及0右边的所有数字. 使得所有数字消失的游戏者获胜. ...
- HBASE+Solr实现详单查询--转
原文地址:https://mp.weixin.qq.com/s?srcid=0831kfMZgtx1sQbzulgeIETs&scene=23&mid=2663994161&s ...
- Collection接口框架
1. Collection接口 其主要的UML类图: Collection接口继承自Iterable接口.Iterable接口中定义了Iterable方法,该方法会返回一个迭代器,用于遍历合集中的元素 ...
- A Magic Lamp HDU - 3183(RMQ返回下标)
原文地址:https://blog.csdn.net/acdreamers/article/details/8692384 题意: 对于一个序列A[1...N],一共N个数,除去M个数使剩下的数组成的 ...
- 【比赛】HNOI2018 毒瘤
虚树+dp 直接看zlttttt的强大题解 zlttttt的题解看这里 #include<bits/stdc++.h> #define ui unsigned int #define ll ...
- 应用程序的日志通过rsyslog推送到syslog服务器
centos5系列系统自带为syslog1.4.1 centos6系列自带rsyslog版本为5.8.10 centos7系列自带rsyslog版本为7.4.7 目前最新版rsyslog为8.27.0 ...
- Django summernote 富文本
Summernote is a simple WYSIWYG editor. GITHUB:https://github.com/summernote/django-summernote SETUP ...
- 使用 nginx 代理 tomcat 服务器
server { listen 80; server_name wechat-jsp.local; root /usr/local/Cellar/tomcat/9.0.5/libexec/webapp ...