HDU-1711
来源:互联网 发布:网络跳线和网线的区别 编辑:程序博客网 时间:2024/06/13 01:23
链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1711
题目:
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
题意:
给你a和b两个数组的长度,然后给你a和b数组,让你求b数组在a数组里第一次出现的位置,下标从1开始。
思路:
KMP水题。
实现:
#include <bits/stdc++.h>using namespace std;const int maxn = int(2e7)+7;int str[maxn], p[maxn], Next[maxn], n, m, t;void GetNext() { for(int q = 1, k = 0 ; q < m ; q++) { while(k > 0 && p[q] != p[k]) k = Next[k-1]; if(p[q] == p[k]) k++; Next[q] = k; }}int KMP() { GetNext(); for(int i = 0, q = 0 ; i < n ; i++) { while(q > 0 && p[q] != str[i]) q = Next[q-1]; if(p[q] == str[i]) q++; if(q == m) return i-m+2; } return -1;}int main() { scanf("%d", &t); while(Next[0] = 0, t--) { scanf("%d%d", &n, &m); for(int i=0 ; i<n ; i++) scanf("%d", str+i); for(int i=0 ; i<m ; i++) scanf("%d", p+i); printf("%d\n", KMP()); } return 0;}
- hdu 1711
- hdu 1711
- hdu 1711
- hdu 1711
- hdu 1711
- hdu 1711
- HDU 1711
- hdu 1711
- HDU 1711
- hdu-1711
- hdu 1711
- HDU 1711
- HDU 1711
- hdu 1711
- hdu 1711
- HDU 1711
- HDU-1711
- hdu 1711
- SQLite数据库升级
- windows+sublime text3+MINGW编译运行c
- oracle数据库常用语句汇总2(修改)
- jpa 使用entitymanager查询表并返回List<Map<Column,Value>>
- not in 两个列表
- HDU-1711
- MyBatis文档观后整理
- 欢迎使用CSDN-markdown编辑器
- 在Ubuntu16.04安装torch7笔记
- 计算2个日期之间的相隔的工作日不算第一天
- Java字符编码原理_动力节点Java学院整理
- Android7.0 PhoneApp的启动
- 实验一:写一个hello world小程序
- 地址转经纬度(百度)function