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. Python中何时使用断言(转)

    原文:http://blog.jobbole.com/76285/ 本文由 伯乐在线 - 贱圣OMG 翻译.未经许可,禁止转载!英文出处:python maillist.欢迎加入翻译小组. 这个问题是 ...

  2. python函数回顾:all()

    描述 all() 函数用于判断给定的可迭代参数 iterable 中的所有元素,是否不为 0.''.False 或者 iterable 为空,如果是返回 True,否则返回 False. 如果是空元组 ...

  3. Linux中的输出重定向

    标准输入输出: 键盘        /dev/stdin        0       标准输入 显示器    /dev/stdout      1       标准输出 显示器    /dev/st ...

  4. 前端基础之css样式(选择器)

    一.css概述 CSS是Cascading Style Sheets的简称,中文称为层叠样式表,对html标签的渲染和布局 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. 例如 二.c ...

  5. Hibernate学习---关联关系映射

    关联关系是用到的最多的一种关系,非常重要,在内存中反映为实体关系,映射到DB中主键外键关系,实体间的关联,即对外键的维护,关联关系的发生,即对外键数据的改变. 在这里就不赘述什么是外键什么是主键了. ...

  6. 生于MVP,死于PMF

    本文的主要内容会按照是什么.为什么以及如何做的逻辑展开,主要包括以下几部分: 什么是MVP与PMF: 为什么要有MVP与PMF: 如何创建MVP: 如何验证PMF. 什么是MVP与PMF MVP(Mi ...

  7. R基本画图

    参考内容:闻博,R语言的绘图功能及应用案例  https://wenku.baidu.com/view/80f22fa50029bd64783e2c22.html R画图是以函数操作为基本的画图模式. ...

  8. cocos2dx打飞机项目笔记二:BulletLayer类

    BulletLayer.h 内容如下 class BulletLayer : public cocos2d::CCLayer { public: CC_SYNTHESIZE(bool, m_IsHer ...

  9. pd.read_csv的header用法

    默认Header = 0: In [3]: import pandas as pd In [4]: t_user = pd.read_csv(r'C:\Users\Song\Desktop\jdd_d ...

  10. 安装MySQL5.7.18遇到的坑

    最近才注意到MySQL的各个版本之间差别还挺大的,比如5.5.x版本的timestamp类型列只能有一个设置为default CURRENT_TIMESTAMP的,于是尝试了换成一个新版本是mysql ...