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

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

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
 
模板练习题
 #include <algorithm>
#include <cstdio> using namespace std; const int N(+);
const int M(+);
int l1,l2,a[N],b[N],p[N]; inline void Get_next()
{
for(int j=,i=;i<=l2;i++)
{
if(j>&&b[i]!=b[j+]) j=p[j];
if(b[i]==b[j+]) j++;
p[i]=j;
}
}
inline void kmp()
{
for(int j=,i=;i<=l1;i++)
{
if(j>&&a[i]!=b[j+]) j=p[j];
if(a[i]==b[j+]) j++;
if(j==l2)
{
printf("%d\n",i-j+);
return ;
}
}
puts("-1");
} int main()
{
int t;scanf("%d",&t);
for(;t--;)
{
scanf("%d%d",&l1,&l2);
for(int i=;i<=l1;i++) scanf("%d",a+i);
for(int i=;i<=l2;i++) scanf("%d",b+i);
Get_next();
kmp();
}
return ;
}

HDU——T 1711 Number Sequence的更多相关文章

  1. HDU 1711 Number Sequence(数列)

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

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

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

  3. HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...

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

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

  5. Rabin_Karp(hash) HDOJ 1711 Number Sequence

    题目传送门 /* Rabin_Karp:虽说用KMP更好,但是RK算法好理解.简单说一下RK算法的原理:首先把模式串的哈希值算出来, 在文本串里不断更新模式串的长度的哈希值,若相等,则找到了,否则整个 ...

  6. HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)

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

  7. HDU 1711 Number Sequence (KMP简单题)

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

  8. hdu 1711 Number Sequence KMP 基础题

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

  9. KMP - HDU 1711 Number Sequence

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

随机推荐

  1. JDBC连接SQL Server 2005 报错Connection refused: connect

    com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败.错误:“Connect ...

  2. Android中System.currentTimeMillis()

    函数: System.currentTimeMillis(): 功能:产生一个当前的毫秒,这个毫秒事实上就是自1970年1月1日0时起的毫秒数,Date()事实上就是相当于Date(System.cu ...

  3. 利用photoshop的动作功能实现图片批处理操作

    首先,通过"窗体菜单"点击"动作".启动动作栏. 然后.通过动作栏的菜单,新建动作. 接着,完毕一个标准操作,包含打开图像,图像处理或者格式转换,另存为,等. ...

  4. hdu1879 继续畅通project(最小生成树)

    继续畅通project Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  5. 当我们谈论Erlang Maps时,我们谈论什么 Part 1

         Erlang 添加 Maps数据类型并非非常突然,由于这个提议已经进行了2~3年之久,仅仅只是Joe Armstrong老爷子近期一篇文章Big changes to Erlang掀起不小了 ...

  6. Delphi的参数修饰const/var/output 与C++的对应关系

    delphi的const/input和默认的没有修饰, C++都是一样的 delphi的var,对应C++那边是指针,  调用方需要管理内存(负责分配内存及销毁) delphi的output , 对应 ...

  7. vue24-webpack+vue-loader

    手动配置自己: webpack+vue-loader webpack加载模块 ------------------------------------- 如何运行此项目? 1. npm install ...

  8. linux关于用户密码家目录总结

    创建用户及其家目录useradd -d /home/tomcat -m tomcat接着修改密码passwd tomcat usermod -s /sbin/nologin + 用户名 禁止登录ssh ...

  9. css3 边框阴影效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. C2mini 摄像头添加 到 7832N 录像机方法

    1.在YS7.com 把2个设备全部添加 (录像机也可以通过手机添加) 2.在录像机里添加摄像头 注: 如出现 “未知错误”可能为录像机版本过低造成.