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. window.location.href 和self.location的区别

    你从字面上就可以理解到 window 指的是当前窗口 而 self 指的是自己 在HTML 中 由于页面可以镶嵌页面 所以这2个就有了 区别 比如说 我有个页面A.HTML 里面嵌套了一个B.HTML ...

  2. 网络断开后重连downloadProvider继续下载问题调试分析

    最近在安卓4.4上遇到一个断开wifi后重新连接wifi, downloadProvider继续下载文件失败的问题.于是开始了解下载管理模块的断点续载功能:     1.首先,分析android lo ...

  3. python的exec、eval详解

    exec exec语句用来执行储存在字符串或文件中的Python语句.例如,我们可以在运行时生成一个包含Python代码的字符串,然后使用exec语句执行这些语句.下面是一个简单的例子. exec ' ...

  4. C# 操作电脑 关机 重启 注销 休止 休眠

    // 关机 强制电脑10秒之内关机 //System.Diagnostics.Process.Start("shutdown", "-s -f -t 10"); ...

  5. Struts2 框架验证

    struts2框架验证(xml方式):    * 首先要从页面中获取对应的标签name属性的值,在动作类action中声明同名的属性,提供get和set方法        * 创建一个xml格式验证文 ...

  6. [转]extern,static存储空间矛盾

    其实,这两个语句的位置不同,会出现不同的解释.这主要是由于 static 具有的两重意义所导致的: (1) 如果 static int foo; 这一句位于函数中,则 static 表示的是存储属性, ...

  7. TComboBox组件的重要属性

    TComboBox组件的重要属性 CharCase--------此属性用于设置编辑框内文字的大小写DropDownCount---此属性用于设置当用户下拉组合框时不需要加滚动条就能显示的项的个数Dr ...

  8. cocos2d-x创建新项目模板

    1.起因 长期使用项目中自带的HelloWorldScene来创建模板工程,不知大家有木有感到厌烦? 我是个懒人,所以就弄了个新的模板工程.这样最起码可以不用每次都把HelloWorldScene删掉 ...

  9. 了解JVM

    ---恢复内容开始--- Java对象在运行环境中,对于内存而言,存在三种状态:年轻代.年老代.永生代: 下图是JVM内存模型 1. 年轻代被分为3个部分:Enden区和两个Survivor区,垃圾回 ...

  10. javascript 关于一周前一个月前的处理方法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...