http://acm.hdu.edu.cn/showproblem.php?pid=1711

Number Sequence

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

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了,注意模板是从下标0开始的我一直输入时从1开始导至答案不对。

 #include<cstring>
#include<cstdio>
#include<map>
#include<iostream>
using namespace std;
map<int,int>M;
int nex[],l1,l2,sl;
int S[],T[];
int solve()
{
int i,j;
nex[]=nex[]=;
for(i=;i<l2;++i)
{
j=nex[i];
while(j&&T[i]!=T[j])j=nex[j];
nex[i+]=T[i]==T[j]?j+:;
}
j=;
for(i=;i<l1;++i)
{
while(j&&S[i]!=T[j])j=nex[j];
if(S[i]==T[j]){
j++;
if(j==l2)return i-l2+;
}
}
return -;
}
int main()
{
int i,j,t,p,x,tmp;
cin>>t;
while(t--){p=;M.clear();
scanf("%d%d",&l1,&l2);
for(i=;i<l1;++i)
{
scanf("%d",&x);
tmp=M[x];
if(!tmp){M[x]=S[i]=++p;}
else S[i]=tmp;
}
for(i=;i<l2;++i)
{
scanf("%d",&x);
tmp=M[x];
if(!tmp){M[x]=T[i]=++p;}
else T[i]=tmp;
}
cout<<solve()<<endl;;
}
return ;
}

HDU 1711 kmp+离散化的更多相关文章

  1. hdu 1711 KMP算法模板题

    题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...

  2. Number Sequence HDU 1711 KMP 模板

    题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #i ...

  3. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

  4. HDU 1711(KMP)字符串匹配

    链接  HDU 1711 Number Sequence KMP 算法 我以自己理解写的,写的不对,不明白的地方海王子出来,一起共同学习: 字符串匹配 就是KMP,一般思想,用一个for循环找开头   ...

  5. HDU - 1711 A - Number Sequence(kmp

    HDU - 1711 A - Number Sequence   Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1 ...

  6. HDU 1711 Number Sequence(KMP)附带KMP的详解

    题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...

  7. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  8. hdu 1686 KMP模板

    // hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...

  9. Cyclic Nacklace HDU 3746 KMP 循环节

    Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len ...

随机推荐

  1. Thrift官方安装手册(译)

    本篇是Thrift官网安装文档的翻译,原地址点击这里.Thrift之前是不支持Windows的.但是似乎0.9版本以后已经支持Window了.介绍了Thrift安装的环境要求以及在centos,Deb ...

  2. F-02 创建财务凭证BAPI

    **.获取抬头参数, documentheader **.项目参数 accountgl = lt_acgl"G/L account item accountreceivable = lt_a ...

  3. Python基础-文件的基本操作

    测试文件fansik内容如下:This is line 1This is line 2This is line 3This is line 4This is line 5This is line 6 ...

  4. corethink功能模块探索开发(四)让这个模块跑起来

    让这个模块跑起来,太费劲了,多半原因是自己太粗心,opencmf.php中“uid”写成了“pid”,de了好几天的bug也没有搞出来,又加上最近发生了些事(brokenhearted)... 上报错 ...

  5. Google Cloud Platfrom中使用Linux VM

    Linkes https://cloud.google.com/compute/docs/quickstart-linuxhttps://console.cloud.google.com/comput ...

  6. range基础

    collapse这个方法是把结束位置抛弃掉,并不是简单的设置到开始位置. 结束位置被抛弃掉以后,只要没有给它重新设置位置,它就一直都会等 于开始位置.即使你修改了开始位置,结束位置还是会在修改后的开始 ...

  7. 【LeetCode】【定制版排序】Sort Colors

    之前转载过一篇STL的sort方法底层详解的博客:https://www.cnblogs.com/ygh1229/articles/9806398.html 但是我们在开发中会根据自己特定的应用,有新 ...

  8. $Java正则表达式基础整理

    (一)正则表达式及语法简介 String类使用正则表达式的几个方法: 正则表达式支持的合法字符: 特殊字符: 预定义字符: 方括号表达式: 圆括号表达式:用于将多个表达式组成一个子表达式,可以使用或运 ...

  9. Java泛型详解(转)

    文章转自  importNew:Java 泛型详解 引言 泛型是Java中一个非常重要的知识点,在Java集合类框架中泛型被广泛应用.本文我们将从零开始来看一下Java泛型的设计,将会涉及到通配符处理 ...

  10. js判断一个数组是否包含一个指定的值

    今天看了一下  有好几种方法  总结一下 1:array.indexOf   此方法判断数组中是否存在某个值,如果存在返回数组元素的下标,否则返回-1 let arr = ['something', ...