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. Eclipse 搭建struts2 spring3 hibernate3环境实战 待完善

    1.struts2 目前是2.3版本,下载地址http://struts.apache.org/download.cgi struts2包 struts2-core-2.3.16.3.jar stru ...

  2. Android图片加载框架最全解析(四),玩转Glide的回调与监听

    大家好,今天我们继续学习Glide. 在上一篇文章当中,我带着大家一起深入探究了Glide的缓存机制,我们不光掌握了Glide缓存的使用方法,还通过源码分析对缓存的工作原理进行了了解.虽说上篇文章和本 ...

  3. 卷积神经网络用于视觉识别Convolutional Neural Networks for Visual Recognition

    Table of Contents: Architecture Overview ConvNet Layers Convolutional Layer Pooling Layer Normalizat ...

  4. IOS学习笔记02---语言发展概述,计算机语言简介.

    IOS学习笔记02---语言发展概述,计算机语言简介. ------------------------------------------------------------------------ ...

  5. Android Studio体验(二)--创建项目和Genymotion试用

    上周日已经体验了一把Android Studio顺便没事点了点其他功能,不过还是从自己创建项目开始说吧,首先我们要熟悉Android Studio中的Project 和 Module 两个概念.And ...

  6. 细数IE6的一串串的恼人bug,附加解决方法!

    1. li在IE中底部3像素的BUG 解决方案:在<li>上加float:left:即可解决 2. IE6中奇数宽高的BUG. 解决方案:就是将外部相对定位的div宽度改成偶数.高度也是一 ...

  7. C++类模板的三种特化

    说起C++的模板及模板特化, 相信很多人都很熟悉 ,但是说到模板特化的几种类型,相信了解的人就不是很多.我这里归纳了针对一个模板参数的类模板特化的几种类型, 一是特化为绝对类型: 二是特化为引用,指针 ...

  8. struts2学习笔记(3)---Action中訪问ServletAPI获取真实类型的Servlet元素

    一.源码: struts.xml文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE s ...

  9. C语言变量的声明位置

    标准C里面必须放在代码前面,否则出错: C++里面不一定要放在最前面,用的时候声明也不迟: 所以要看具体的编译环境,如果是C的话必须放在最前,C++就不用:一般.c后缀的是C文件,按C来编译:.cpp ...

  10. js replace全部替换的方法

    1.JS replace()方法替换变量(可以对变量进行全文替换) string.replace(new RegExp(key,'g'),"b"); 2.封装 String.pro ...