hdu 1711 Number Sequence KMP 基础题
来源:互联网 发布:淘宝客返利api申请 编辑:程序博客网 时间:2024/05/22 17:15
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11691 Accepted Submission(s): 5336
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
213 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 1 313 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 2 1
Sample Output
6-1
Source
HDU 2007-Spring Programming Contest
Recommend
lcy | We have carefully selected several similar problems for you: 3336 3746 2203 1710 3068
题意:给两个数组a和b,求b在a中出现的的第一个位置,若没有则输出-1,只是数组变成了整型数组,方法与字符数组完全一样。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 1000005#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6typedef long long ll;using namespace std;int s[maxn],p[maxn],nextval[maxn];int N,M;void get_nextval(){ int i=0,j=-1; nextval[0]=-1; while (i<M) { if (j==-1||p[i]==p[j]) { i++; j++; if (p[i]!=p[j]) nextval[i]=j; else nextval[i]=nextval[j]; } else j=nextval[j]; }}int KMP(){ int i=0,j=0; while (i<N&&j<M) { if (j==-1||s[i]==p[j]) { i++; j++; } else j=nextval[j]; } if (j==M) return i-M+1; else return -1;}int main(){ int cas; scanf("%d",&cas); while (cas--) { scanf("%d%d",&N,&M); for (int i=0;i<N;i++) scanf("%d",&s[i]); for (int i=0;i<M;i++) scanf("%d",&p[i]); get_nextval(); int ans=KMP(); printf("%d\n",ans); } return 0;}/*213 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 1 313 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 2 1*/
22 0
- hdu 1711 Number Sequence KMP 基础题
- [KMP模板题]HDU-1711 Number Sequence
- hdu 1711 Number Sequence KMP模板题~~~
- hdu 1711 Number Sequence KMP模板题
- hdu 1711 Number Sequence(kmp模板题)
- hdu 1711Number Sequence(kmp模板题)
- hdu 1711 Number Sequence KMP模板题
- Hdu 1711 Number Sequence (kmp模板题)
- hdu 1711 Number Sequence KMP入门题
- HDU 1711 Number Sequence(kmp)
- HDU 1711 Number Sequence (kmp)
- hdu 1711 Number Sequence--KMP
- hdu kmp 1711 Number Sequence
- HDU 1711 Number Sequence (KMP)
- HDU 1711Number Sequence(KMP)
- Hdu 1711 KMP Number Sequence
- HDU 1711 Number Sequence KMP
- [HDU 1711]Number Sequence[kmp]
- 360公司2015年秋季校园招聘笔试考卷(技术类 D)部分试题程序验证和解析4
- VB Winsock 控件TCP与UDP连接实例
- 使用sysbench对Oracle做基准测试(二) CPU
- 在mysql里面建立新的user,pw,db
- 每天一个linux命令(18):locate 命令
- hdu 1711 Number Sequence KMP 基础题
- Ubuntu - 硬盘分区、格式化、自动挂载配置(转)
- MIPS指令系统的原子交换功能
- Spring配置文件 applicationContext.xml 详解
- 启动U盘制作
- Android开发之路——四大核心组件1 Activity(1)生命周期
- 男人出轨与X的理解2014.10.19
- Microbe开发日志--前言
- [译文]JOAL教程 第三课 多声源播放