hdu--1711--kmp应用在整形数组--Number Sequence

/*
    Name: hdu--1711--Number Sequence
    Author: shen_渊
    Date: 16/04/17 19:58
    Description: 第一次知道,KMP能用在整形数组 o(╯□╰)o
*/

#include<cstring>
#include<iostream>
using namespace std;
int kmp();
void getFail();
int n,m;
int s1[1000009],s2[10009];
int f[10009];
int main()
{
//    freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    while(T--){
        memset(s1,0,sizeof(0));
        memset(s2,0,sizeof(0));
        memset(f,0,sizeof(0));
        cin>>n>>m;
        for(int i=0; i<n; ++i)cin>>s1[i];
        for(int i=0; i<m; ++i)cin>>s2[i];
        if(n < m)cout<<"-1\n";
        else cout<<kmp()<<endl;
    }
    return 0;
}
void getFail(){
    f[0] = 0;f[1] = 0;
    for(int i=1; i<m; i++){
        int j = f[i];
        while(j && s2[i] != s2[j]) j = f[j];
        f[i+1] = s2[i] == s2[j] ? j+1:0;
    }
}
int kmp() {
    getFail();
    int j=0;
    for(int i=0; i<n; ++i){
        while(j && s2[j] != s1[i]) j=f[j];
        if(s2[j] == s1[i]) j++;
        if(j == m) return i-m+2;
    }
    return -1;
}
时间: 2024-10-05 08:19:56

hdu--1711--kmp应用在整形数组--Number Sequence的相关文章

hdu 1711 KMP模板题

// hdu 1711 KMP模板题 // 贴个KMP模板吧~~~ #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int MAX_N = 1000008; const int MAX_M = 10008; int T[MAX_N]; int p[MAX_M]; int f[MAX_M]; int

hdu 1711 KMP算法模板题

题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串长度". 当发生失配的情况下,j的新值next[j]取决于模式串中T[0 ~ j-1]中前缀和后缀相等部分的长度, 而且next[j]恰好等于这个最大长度. 防止超时.注意一些细节.. 另外:尽量少用strlen.变量记录下来使用比較好,用字符数组而不用string //KMP算法模板题 //hdu

HDU 1711 kmp+离散化

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

Number Sequence HDU 1711 KMP 模板

题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<stdio.h> #include<stdlib.h> #include<queue> #include<m

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

Number Sequence(快速幂矩阵)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 131753    Accepted Submission(s): 31988 Problem Description A number sequence

HDU 1711 Number Sequence(KMP算法)

题目链接: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): 15548    Accepted Submission(s): 6836 Problem Description Given two sequence

KMP算法的定义及KMP练手题 HDU 1711 Number Sequence (我的模板代码)

题意:就是要你来找出b数组在a数组中最先匹配的位置,如果没有则输出-1 思路:直接KMP算法(算法具体思想这位牛写的不错http://blog.csdn.net/v_july_v/article/details/7041827) AC代码: #include<cstdio> #include<cstring> #include<stdlib.h> #include<iostream> using namespace std; #define maxn 100

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], 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[