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. 【IUML】支持向量机SVM[续]

    支持向量机基本上是最好的有监督学习算法了.看很多正统的讲法都是从VC 维理论和结构风险最小原理出发,然后引出SVM什么的,还有些资料上来就讲分类超平面什么的.我们logistic回归出发,引出了SVM ...

  2. 第十八章 springboot + thymeleaf

    代码结构: 1.ThymeleafController package com.xxx.firstboot.web; import org.springframework.stereotype.Con ...

  3. 第十三章 springboot + lombok

    lombok作用:消除模板代码. getter.setter.构造器.toString().equals() 便捷的生成比较复杂的代码,例如一个POJO要转化成构建器模式的形式,只需要一个注解. 注意 ...

  4. 关于VS 工具箱灰色,不可用的解决方案

    使用vs的命令行工具,在命令行中运行:devenv /ResetSkipPkgs ,重新打开vs,重置一下工具箱 ,OK,成功了~! 希望能对大家有帮助!

  5. WordPress 后台添加额外选项字段到常规设置页面

    有时候我们需要添加一些额外的设置选项到常规设置(后台 > 设置 > 常规)页面,下面是一个简单的范例: 直接添加到主题的 functions.php 即可:   /*** WordPres ...

  6. java web过滤器实际应用(解决中文乱码 html标签转义功能 敏感字符过滤功能)

    转载地址:http://www.cnblogs.com/xdp-gacl/p/3952405.html 在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可 ...

  7. 11个实用的CSS学习工具

    1. 盒子模型的幻灯片 通过3D转换效果产生的互动的幻灯片.按向左或向右箭头键切换,全屏观看会有更好的效果. 2. CSS Diner 通过一个简单的小游戏让你学习CSS selector,输入正确的 ...

  8. JS 提交form表单

    源码实例一:javascript 页面加裁时自动提交表单Form表单:<form method="post" id="myform" action=&qu ...

  9. ZH奶酪:C语言中malloc()和free()函数解析

    1.malloc()和free()的基本介绍 (1)函数原型及说明 void *malloc(long NumBytes) 该函数分配了NumBytes个字节,并返回了指向这块内存的指针.如果分配失败 ...

  10. c的链接详解

    多目标文件的链接 stack.c #include <stdio.h> #define STACKSIZE 1000 typedef struct stack { int data[STA ...