KMP算法

KMP的基处题目,数字数组的KMP算法应用。

主要是next[]数组的构造,next[]存储的是字符的当前字串,与子串前字符匹配的字符数。

移动位数 = 已匹配的字符数 - 对应的部分匹配值

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

HDU 2007-Spring Programming Contest
 #include<stdio.h>
int a[],b[],next[];
void getnext(int m){
int i=,j=;
next[]=;
while(i<m){
if(j==||b[i]==b[j]){
i++; j++; next[i]=j;
}
else j=next[j];
}
} void getk(int n,int m){
int i=,j=;
while(i<=n&&j<=m){
if(j==||a[i]==b[j]){i++; j++;}
else j=next[j];
}
if(j>m) printf("%d\n",i-m);
else printf("-1\n");
} int main()
{
int t,n,m,i,j;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(i=;i<=n;i++) scanf("%d",&a[i]);
for(i=;i<=m;i++) scanf("%d",&b[i]);
getnext(m);
getk(n,m);
}
return ;
}

【KMP】Number Sequence的更多相关文章

  1. 【HDU1711】Number Sequence

    题面 大致题意: 给定两个数列A,B,长度分别为N和M 求出 满足 Ak=B1 ,Ak+1=B2......Ak+M-1=Bm 的最小k值 如果有多个k值输出最小的一个 题解 KMP裸题 直接计算B数 ...

  2. 【poj1019】 Number Sequence

    http://poj.org/problem?id=1019 (题目链接) 题意 给出一个数:1 12 123 1234 12345 123456 1234567 12345678 123456789 ...

  3. 【hdu1005】Number Sequence

    题目描述 一个数列的定义如下: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A和B,你要求出f(n). 输入 输 ...

  4. 【KMP】【最小表示法】NCPC 2014 H clock pictures

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...

  5. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  6. 【CF1151E】Number of Components

    [CF1151E]Number of Components 题面 CF 题解 联通块个数=点数-边数. 然后把边全部挂在较小的权值上. 考虑从小往大枚举左端点,等价于每次删掉一个元素,那么删去点数,加 ...

  7. 【arc071f】Infinite Sequence(动态规划)

    [arc071f]Infinite Sequence(动态规划) 题面 atcoder 洛谷 题解 不难发现如果两个不为\(1\)的数连在一起,那么后面所有数都必须相等. 设\(f[i]\)表示\([ ...

  8. 【arc074e】RGB Sequence(动态规划)

    [arc074e]RGB Sequence(动态规划) 题面 atcoder 洛谷 翻译见洛谷 题解 直接考虑暴力\(dp\),设\(f[i][j][k][l]\)表示当前考虑到第\(i\)位,最后一 ...

  9. 【BZOJ3275】Number 最小割

    [BZOJ3275]Number Description 有N个正整数,需要从中选出一些数,使这些数的和最大.若两个数a,b同时满足以下条件,则a,b不能同时被选1:存在正整数C,使a*a+b*b=c ...

随机推荐

  1. IntelliJ IDEA 中module的dependencies是其它module时的注意事项

    Dependencies on other modules If a module (module A) depends on another module (module B), IntelliJ ...

  2. 0x05: post 守护进程(deamon) json 任务调度

    python 签名 post #coding:utf-8 import urllib,urllib2 url='http://wtf.thinkphp.com/index.php?m=&c=t ...

  3. Everything 使用技巧

    使用技巧 可以使用逻辑符空格(与), |(或),!(非),及通配符 * 和 ? ,符号或搜索内容之间必须加空格且空格任意使用如果经常搜索同一内容,可以使用书签功能,且可以对书签进行添加.编辑.排序.导 ...

  4. html跳转到同一个页面的不同位置

    <html> <body> <p><a href="#C4">查看 Chapter 4.</a></p> & ...

  5. MySQL性能调优与架构设计读书笔记

    可扩展性设计之数据切分 14.2 数据的垂直切分 如何切分,切分到什么样的程度,是一个比较考验人的难题.只能在实际的应用场景中通过平衡各方面的成本和利益,才能分析出一个真正适合自己的拆分方案. 14. ...

  6. C# - 使用 OLEDB读取 excel(不用Excel对象).

    参考: How to read from an Excel file using OLEDB 为了使用方便,我做成了工具类(OledbCommon.cs),好以后使用. 注:连接字符串中,Provid ...

  7. RTX管理器服务运行状态空白

    A)打开RTX管理器安装目录下的bin文件夹,运行convert.bat 右键计算机——管理——服务——找到以RTX开头的服务,按RTX_ConfigCenter.RTX_HTTPServer. RT ...

  8. MySQL利用binlog来恢复数据库

    1.根据binlog解析出所有ring数据库的所有sql [mysql@localhost ]$ mysqlbinlog --no-defaults --database=ring --start-d ...

  9. MySQL查询优化处理

    查询的生命周期的下一步是将一个sql转化成一个执行计划,MySQL再依照这个执行计划和存储引擎进行交互.这包括多个子阶段:解析sql,预处理,优化sql执行计划.这个过程中任何错误(例如语法错误)都可 ...

  10. CentOS 7 之几个新特性(转)

    上篇我们讲到默认没有ifconfig是centos7的新特性,所以我特意上网搜索了一下其新特性,找到一篇文章,现转过来. centos最小好化安装没有ifconfig命令 刚安装了centos7.0, ...