HDU 1711 Number Sequence (字符串匹配,KMP算法)
HDU 1711 Number Sequence (字符串匹配,KMP算法)
Description
Given two sequences of numbers : a1, a2, ...... , aN, and b1, b2, ...... , bM (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make aK = b1, aK+1 = b2, ...... , aK+M−1 = bM. 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 a1, a2, ...... , aN. The third line contains M integers which indicate b1, b2, ...... , bM. 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
Http
HDU:https://vjudge.net/problem/HDU-1711
Source
字符串匹配,KMP算法
解决思路
就是KMP算法的运用,请参看我的这篇文章。
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxN=1000001;
const int inf=2147483647;
int n,m;
int A[maxN];
int B[maxN];
int F[maxN];
int read();
int main()
{
int TT=read();
for (int ti=1;ti<=TT;ti++)
{
n=read();
m=read();
for (int i=0;i<n;i++)
A[i]=read();
for (int i=0;i<m;i++)
B[i]=read();
F[0]=-1;
for (int i=1;i<m;i++)
{
int j=F[i-1];
while ((B[j+1]!=B[i])&&(j!=-1))
j=F[j];
if (B[j+1]==B[i])
F[i]=j+1;
else
F[i]=-1;
}
//for (int i=0;i<m;i++)
// cout<<F[i]<<' ';
//cout<<endl;
int i=0,j=0;
bool flag=0;
while (i<n)
{
if (A[i]==B[j])
{
i++;
j++;
if (j==m)
{
cout<<i-j+1<<endl;
flag=1;
break;
}
}
else
if (j==0)
i++;
else
j=F[j-1]+1;
}
if (flag==0)
cout<<-1<<endl;
}
return 0;
}
int read()
{
int x=0;
int k=1;
char ch=getchar();
while (((ch<'0')||(ch>'9'))&&(ch!='-'))
ch=getchar();
if (ch=='-')
{
k=-1;
ch=getchar();
}
while ((ch<='9')&&(ch>='0'))
{
x=x*10+ch-48;
ch=getchar();
}
return x*k;
}
HDU 1711 Number Sequence (字符串匹配,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(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- 字符串匹配KMP算法详解
1. 引言 以前看过很多次KMP算法,一直觉得很有用,但都没有搞明白,一方面是网上很少有比较详细的通俗易懂的讲解,另一方面也怪自己没有沉下心来研究.最近在leetcode上又遇见字符串匹配的题目,以此 ...
- 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应用 求成功匹配子串的最小下标】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...
- 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 基础题
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], .... ...
随机推荐
- Bitcoin Core P2P网络层
目录 数据结构 节点发现和节点连接 地址管理 节点发现 节点连接 插口(Sockets)和消息 Socket线程 (net.cpp) 消息线程 ProcessMessages (net_process ...
- python3.6环境中django2.0与xadmin0.6结合的后台管理
1.xadmin简介 django的admin管理后台页面很简洁,对个人来说做后台管理非常简单:xadmin的比较admin优化界面,看着也舒服. xadmin界面效果如下: 2.xadmin安装 从 ...
- 深入 JSX
从本质上讲,JSX 只是为 React.createElement(component, props, ...children) 函数提供的语法糖.JSX代码: 1 2 3 <MyButton ...
- centos7安装oracle亲测可用
http://www.linuxidc.com/Linux/2016-04/130559p2.htm
- 如何使用g++编译调用dll的c++代码
本文将有以下4个部分来讲如何使用g++编译调用dll的c++代码. 1.如何调用dll 2.动态链接和静态链接的区别 3.g++的编译参数以及如何编译调用dll的c++代码 4.总结 1.如何调用dl ...
- 11.15 Daily Scrum
今天是假期回来的第一个周末,也是我们团队的又一次进度汇总总结和调试工作开展,鉴于一周以来大家的工作有了很大的成果,所以,本次召开的会议主旨在于解决一些开发方面的细节问题,达成共识,为日后进一步的功能方 ...
- Chapter 3 软件项目管理
软件项目具有产品的不可见性.项目的高度不确定性.软件过程的多变化性.软件人员的高流动性的显著特征.有效的软件项目管理集中于人员.产品.过程和项目四个方面.软件项目的生命周期有项目启动.项目规划.项目实 ...
- servlet的方法解析
一般来说servlet继承了HttpServlet,我们可以覆盖某些方法来实现自己的功能. Init()和Init(ServletConfig config),我们一般只需覆盖后者,因为这个可以从se ...
- Software-Defined Networking A Comprehensive Survey(一)
传统网络:1 复杂,难于管理 2 很难实现根据之前定义的方案进行配置,3 对于缺陷.变化不能够再次进行配置 4 控制和数据平面绑定在一起,使许多缺陷难于解决 SDN网络:通过打破传统网络垂直整合,从底 ...
- springboot maven
更多信息请从官网获取https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE 1.parent基于自己项目而非spring-boot-starter- ...