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
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
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3336 3746 

pid=2203" target="_blank">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-6
typedef 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;
}
/*
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
*/

最新文章

  1. Android Studio的优化/Gradle构建
  2. javascript中的克隆
  3. php-fpm服务挂掉
  4. 纯CSS多级菜单
  5. 在centos7上作用mongodb
  6. 【MFC】无边框窗体 WS_THICKFRAME
  7. Apache2 添加登陆用户名和密码
  8. zlib用法说明
  9. C# 调用第三方DLL完整实例
  10. IAR stm8带库的工程模板
  11. Response
  12. Sublime Text 3插件安装方法
  13. windows bat脚本编写
  14. 让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET
  15. iOS 之 #import与#include的区别及@class
  16. HelloWorld改编,仿bilibili手机端(一)——侧滑菜单界面布局
  17. CSS 权威指南 CSS实战手册 第四版(阅读笔记)
  18. C语言实现过滤ASCII在0~127范围内的字符,并去除重复的字符
  19. 9. Web browser-related (网页浏览器相关 4个)
  20. AI 积分图

热门文章

  1. kevinekline----------------- SQLSERVER MVP
  2. VHD命令
  3. 万里长征第二步——django个人博客(第三步 —— 设置一些全局变量)
  4. [转]初试visual studio2012的新型数据库LocalDB 及 在visual studio2012中如何使用localDB具体讲解
  5. iOS:多线程的详细介绍
  6. iOS:iOS中的几种动画
  7. Android-ImageView的属性android:scaleType作用
  8. 如何在 Ubuntu 上搭建网桥
  9. 浅记初次使用expect、scp中出现的一些小问题
  10. Qracle、Sql server 、mysql查询练习题