[kmp]HDU1711 Number Sequence

题目大意

题意:给T组数据,每组有长度为n和m的母串和模式串。判断模式串是否是母串的子串,如果是输出最先匹配完成的位置,否则输出-1.

思考

直接套用模板。把char改成int。kmp函数中在模式串遍历到结尾的时候return,若没遍历到结尾,也就是不是子串返回-1

#include <iostream>
#include  <cstdio>
#include <cstring>
using namespace std;
int nexta[10005],a[1000005],s[10005];
int n,m;
void getfail(int* p,int* f)
{
    f[0]=f[1]=0;
    for(int i=1;i<m;i++)
    {
        int j=f[i];
        while(j&&p[j]!=p[i]) j=f[j];
        f[i+1]=(p[i]==p[j])?j+1:0;
    }
}

int kmp(int* t,int* p,int* f)
{
    getfail(p,f);
    int j=0;
    for(int i=0;i<n;i++)
    {
        while(j&&p[j]!=t[i]) j=f[j];
        if(p[j]==t[i]) j++;
        if(j==m)
        {
            return i-j+2;
        }
    }
    return -1;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i = 0;i < n; i ++)
        {
            scanf("%d",&a[i]);
        }
        for(int j = 0; j < m;j ++)
        {
            scanf("%d",&s[j]);
        }
        printf("%d\n",kmp(a,s,nexta));
    }
    return 0;
}  
时间: 2024-09-28 22:37:36

[kmp]HDU1711 Number Sequence的相关文章

kuangbin专题十六 KMP&amp;&amp;扩展KMP HDU1711 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[K + M - 1] = b[M]. If there are

hdu1711 Number Sequence kmp应用

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目: 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 m

HDU1711 Number Sequence KMP

欢迎访问~原文出处--博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU1711 题意概括 给T组数据,每组有长度为n和m的母串和模式串.判断模式串是否是母串的子串,如果是输出最先匹配完成的位置,否则输出-1. 题解 KMP裸题. 代码 #include <cstring> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cmath>

HDU1711 Number Sequence(KMP模版题)

匹配子串 #include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <vector> #include <cstdio> int a[1000005],b[10005]; int Next[10005]; int n,m; void setNext() { int i=0,j=

hdu1711 Number Sequence(KMP水题)

题意: 在a串中寻找第一个包含b串的的位置 思路:直接KMP即可 #include <cstdio> #define MAXN 1000010 using namespace std; void kmp_pre(int x[],int m,int next[]){ int i,j; j=next[0]=-1; i=0; while(i<m){ while(-1!=j&&x[i]!=x[j]) j=next[j]; if(x[++i]==x[++j]) next[i]=ne

【KMP】Number Sequence

KMP算法 KMP的基处题目,数字数组的KMP算法应用. 主要是模式串的处理,当模式串内有重复时,模式串向左回溯重复的点的位置(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

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

hdoj 1711 Number Sequence 【KMP】

Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11817    Accepted Submission(s): 5395 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1],