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 ------------------------------------------------------我是分割线^_^--------------------------------------------------- 首先,我不得不说对这个算法还有很多的迷糊,不过已经会大概的模板的,记住kmppre的数组如何做
出来的就差不多了,就是当失去匹配时归回的匹配位置,路还有很远,哎= =
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<cctype>
#include<set>
#include<map>
#include<sstream>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define INF 0x3f3f3f3f
#define Int __int64
#define pii pair<int,int>
#define check(x) cout<<"["<<x<<"]"<<endl;
const int MAXN = 1111111;
int num[MAXN];
int kmp_pre[MAXN];
int src[MAXN];
void Kmp_Pre(int num[], int len, int kmp_pre[]) {
    int i = 0, j = 0;
    j = kmp_pre[0] = -1;
    while (i < len) {
        while (j != -1 && num[i] != num[j]) {
            j = kmp_pre[j];
        }
        kmp_pre[++i] = ++j;
    }
}
int Kmp(int num[], int len1, int src[], int len2, int kmp_pre[]) {
    int i = 0, j = 0;
    Kmp_Pre(num, len1, kmp_pre);
    while (i < len2) {
        while (j != -1 && num[j] != src[i]) {
            j = kmp_pre[j];
        }
        i++;
        j++;
        if (j == len1) {
            return i - j + 1;
        }
    }
    return -1;
}
int main() {
    //freopen("input.txt", "r", stdin);
    int T;
    while (scanf("%d", &T) != EOF) {
        while (T--) {
            int n, m;
            scanf("%d %d", &n, &m);
            for (int i = 0; i < n; i++) {
                scanf("%d", &src[i]);
            }
            for (int i = 0; i < m; i++) {
                scanf("%d", &num[i]);
            }
            int ans = Kmp(num, m, src, n, kmp_pre);
            printf("%d\n", ans);
        }
    }
    return 0;
}
 

KMP匹配算法 - Number Sequence的更多相关文章

  1. 【KMP】Number Sequence

    KMP算法 KMP的基处题目,数字数组的KMP算法应用. 主要是next[]数组的构造,next[]存储的是字符的当前字串,与子串前字符匹配的字符数. 移动位数 = 已匹配的字符数 - 对应的部分匹配 ...

  2. kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence

    Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...

  3. [裸KMP][HDU1711][Number Sequence]

    题意 找到子串在母串出现的第一个位置 解法 裸的KMP 特别的地方 第一次不看模板自己敲的KMP #include<stdio.h> const int maxn=100000; cons ...

  4. KMP - HDU 1711 Number Sequence

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

  5. Number Sequence(kmp)

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

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

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

  7. HDU1711 Number Sequence(KMP模板题)

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

  8. (KMP 模板)Number Sequence -- Hdu -- 1711

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

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

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

随机推荐

  1. linux几个常用的命令及获取帮助的方法

    date:时间管理,可以显示.修改系统时间.  设定时间:格式:MMDDhhmm[[cc]yy][.ss]其中MM为月分,DD为日期,hh为小时,mm为分钟,CC为年的前两位 YY为年分的后两位,.s ...

  2. 由表单验证说起,关于在C#中尝试链式编程的实践

    在web开发中必不可少的会遇到表单验证的问题,为避免数据在写入到数据库时出现异常,一般比较安全的做法是前端会先做一次验证,通过后把数据提交到后端再验证一次,因为仅仅靠前端验证是不安全的,有太多的htt ...

  3. ORA-00257: archiver error. Connect internal only, until freed.

    早上BA抄送客户的邮件过来,说系统用不了,应用系统报异常Unable to open connection to oracle,Microsoft Provider v2.0.50727.42,既然是 ...

  4. mysql 基础使用

    mysql服务器本地root用户默认没有密码,使用 "mysql -u root -p" 即可登陆.linux本地用户可以以任意用户名登陆mysql,但是没有任何权限,没有意义.m ...

  5. C#上传图片

    //一般处理程序 public void GetImageFromWeb() { //创建文件夹 //2016-10-14 dq string filePath = "~/ProductIm ...

  6. 悬浮TabBar的实现--此段代码来自网络

    悬浮TabBar的实现 这个TabBar看着像是用自定义TabBar做的,但事实上它还是用的系统的TabBar,给系统的tabBar.backgroundImage设置一张设计好的背景图片. TabB ...

  7. javaScript判断浏览器类型

    <script type="text/javascript"> function getBrowserInfo(){ var OsObject=navigator.us ...

  8. 模拟搭建Web项目的真实运行环境(六)

    今天把Redis和Mongodb的操作整理一下,方便日后自己查看,废话不多说,直接进入主题. 一.Redis 1. 引用StackExchange.Redis.dll 由于ServiceStack.R ...

  9. 移动端下拉刷新、加载更多插件dropload.js(基于jQuery/Zepto)[转]

    使用方法 引用css和js <link rel="stylesheet" href="../dist/dropload.min.css"> < ...

  10. VS 2013编译64位版本QT 4.8.6及使用cmake为依赖QT生成VS项目时Could NOT find Qt4

    对于一些已经解决的问题,本博客不再讨论.只将本人遇到的问题做简单的说明. 一.VS 2013编译64位版本QT 4.8.6 QT项目官网中,对于QT4,其只提供了windows X86的版本,并且支持 ...