Number Sequence

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9114    Accepted Submission(s): 4166

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
kmp 基础
代码:
 /*@kmp扩展@龚细军*/
#include<stdio.h>
#include<string.h>
int aa[],bb[];
int next[];
//依旧使用next数组
void get_next(int *pt,int len)
{
memset(next,,sizeof(next));
int i=,j=-;
next[]=-;
while(i<len)
{
if(j==-||pt[i]==pt[j])
{
++i;
++j;
if(pt[i]!=pt[j])
next[i]=j;
else
next[i]=next[j];
}
else
j=next[j];
}
}
//kmp的扩展
int exd_kmp(int *ps,int *pt,int lens,int lent)
{
int i=-,j=-;
get_next(pt,lent);
while(i<lens)
{
if(j==-||ps[i]==pt[j])
{
++i;
++j;
}
else
j=next[j];
if(j==lent)break;
}
if(j==lent)
return i-j+;
else
return -;
} int main()
{
int test,n,m,i;
scanf("%d",&test);
while(test--)
{
scanf("%d%d",&n,&m);
for(i=;i<n;i++)
scanf("%d",&aa[i]);
for(i=;i<m;i++)
scanf("%d",&bb[i]);
printf("%d\n",exd_kmp(aa,bb,n,m));
}
return ;
}

java代码:

 import java.util.Scanner;

 public class Main {

     public static void main(String args [])
{
mt aa = new mt();
Scanner reader =new Scanner(System.in);
int test=reader.nextInt();
while((test--)>0)
{
aa.lena=reader.nextInt();
aa.lenb=reader.nextInt();
aa.init(aa.lena+1, aa.lenb+1);
for(int i=0;i<aa.lena;i++)
aa.a[i]=reader.nextInt();
for(int i=0;i<aa.lenb;i++)
aa.b[i]=reader.nextInt();
kmp(aa);
}
}
private static void kmp(mt aa)
{
int i,j;
aa.next[i=0]=-1;
j=-1;
while(i<aa.lenb)
{
if(j==-1||aa.b[i]==aa.b[j])
{
i++;
j++;
if(aa.b[i]==aa.b[j])
aa.next[i]=aa.next[j];
else
aa.next[i]=j;
}
else
j=aa.next[j];
}
i=j=0;
while(i<aa.lena&&j<aa.lenb)
{
if(j==-1||aa.a[i]==aa.b[j])
{
i++;
j++;
}
else j=aa.next[j];
}
if(j==aa.lenb)
out(i-aa.lenb+1);
else
out(-1);
}
private static void out(int aa)
{
System.out.println(aa);
}
}
class mt
{
int [] b,a,next;
int lena,lenb;
void init(int lena,int lenb)
{
a=new int [lena];
b=new int [lenb];
next =new int [lenb];
}
}

HDUOJ------1711Number Sequence的更多相关文章

  1. hduoj 2062Subset sequence

    Subset sequence Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. HDUOJ Number Sequence 题目1005

     /*Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  3. hdu 1711Number Sequence (KMP入门,子串第一次出现的位置)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. hdu 1711Number Sequence

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 数字KMP,原来还能这么用 #include<stdio.h> ],b[]; ]; ...

  5. HDU 1711Number Sequence【KMP模板题】

    <题目链接> 题目大意: 意思是给出两个串,找出匹配串在模式串中的位置. 解题分析: KMP算法模板题. #include <cstdio> #include <cstr ...

  6. oracle SEQUENCE 创建, 修改,删除

    oracle创建序列化: CREATE SEQUENCE seq_itv_collection            INCREMENT BY 1  -- 每次加几个              STA ...

  7. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  8. DG gap sequence修复一例

    环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...

  9. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  10. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

随机推荐

  1. mac下使用brew安装java等应用

    可以使用brew安装很多应用,比如java,idea,iterms,sublime brew tap caskroom/versions 将会安装新的brew仓库源brew cask install ...

  2. 用过Retina视网膜屏幕的笔记本电脑的后果

    用过Retina视网膜屏幕的笔记本电脑的后果是过程中感觉很不错,但是结果是普通屏幕再也看不上眼了.发现了原来看的好好的屏幕多出了许多的像素点,没办法,火眼金睛了.

  3. 数学图形之Boy surface

    这是一个姓Boy的人发现的,所以取名为Boy surface.该图形与罗马图形有点相似,都是三分的图形.它甚至可以说是由罗马曲面变化而成的. 本文将展示几种Boy曲面的生成算法和切图,使用自己定义语法 ...

  4. ubuntu/wireshark: There are no interfaces on which a capture can be done.故障解决

    [转载]http://blog.csdn.net/ccwwff/article/details/6697258 在ubuntu安装wireshark, 在启动程序启动wireshark. 点captr ...

  5. Cesium中Homebutton改变默认跳转位置 【转】

    在Cesium中,Homebutton的默认跳转位置是美国,那么在开发中我们如何更改这个默认跳转位置呢,这就要更改一下源代码了: Camera.DEFAULT_VIEW_RECTANGLE = Rec ...

  6. 让两个DIV的高度隐式同步

    以前遇到两个相临近的块,高度要一样,但是内容多少又不定时,我都是通过把这两块封装在TD里面实现,但今天在CSDN上面看到有人要通过JS来实现这个,我尝试了一下.http://topic.csdn.ne ...

  7. Cognos由于JAVA_HOME冲突引起的错误假象

    Cognos的安装和配置并不是很复杂,但是对于初次安装的用户来说,还是要注意一些细节,比如JDK问题,今天我们就来阐述一下这个问题 场景1: 作为一个开发人员,很多人是十八般武艺样样精通,难免已经在自 ...

  8. MFC COM调用时出现E_OUTOFMEMORY错误

    按照<com原理与应用>第五章写的基于MFC dll的COM,COM对象不是基于Automation的,自己映射了接口,也把潘爱民的源代码看了,感觉和他的代码一样呀,为什么在客户端用CoC ...

  9. 在项目代码中载入cocostudio导出的动画并循环播放

    须要在代码中引入#include "cocostudio/CocoStudio.h" using namespace cocostudio; ArmatureDataManager ...

  10. (算法)Trapping Rain Water II

    题目: Given n * m non-negative integers representing an elevation map 2d where the area of each cell i ...