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]=next[j];
        else
            next[i]=j;
    }
}
int next[10010];
int kmp(int x[],int m,int y[],int n){
    kmp_pre(x,m,next);
    int i,j;
    i=j=0;
    while(i<n){
        while(-1!=j&&y[i]!=x[j]) j=next[j];
        ++i,++j;
        if(j>=m){
            return i-m+1;
        }
    }
    return -1;
}
int y[MAXN];
int x[10010];
int n,m;
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;++i)
            scanf("%d",&y[i]);
        for(int i=0;i<m;++i)
            scanf("%d",&x[i]);
        printf("%d\n",kmp(x,m,y,n));
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-08 14:14:29

hdu1711 Number Sequence(KMP水题)的相关文章

hdu 1711 Number Sequence KMP 基础题

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

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应用

题目地址: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

hdu 1711 Number Sequence KMP模板题

题意:给你两个数组,求第二个数组在第一个数组中的位置,若不存在,则输出-1. #include <cstdio> #include <cmath> #include <iostream> #include <string.h> #include <algorithm> using namespace std; int d[10005]; int f[1000005]; int Next[20005]; void KMP(int s[],int l

hdu 1711 Number Sequence(KMP)

# include <stdio.h> # include <string.h> # include <algorithm> using namespace std; int n,m,next[10010],a[1000010],b[10010]; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<m) { if(j==-1||b[i]==b[j]) i++,j++,next[i]=j; else j=next[

HDU 1711 Number Sequence KMP题解

KMP查找整数数列,不是查找字符串. 原理是一样的,不过把字符串转换为数列,其他基本上是一样的. #include <stdio.h> #include <string.h> const int MAX_N = 1000001; const int MAX_M = 10001; int strN[MAX_N], strM[MAX_M], next[MAX_M], N, M; void getNext() { memset(next, 0, sizeof(int) * M); for

POJ 2081 Recaman&#39;s Sequence(水题)

[题意简述]:这个题目描述很短,也很简单.不再赘述. [分析]:只需再加一个判别这个数是否出现的数组即可,注意这个数组的范围! // 3388K 0Ms #include<iostream> using namespace std; #define Max 500001 int a[Max]; bool b[10000000] = {false}; // b的数据范围是可以试出来的- void init() { a[0] = 0; b[0] = true; for(int m = 1;m<

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 1.1)hdu 1711 Number Sequence(KMP的简单应用——求pattern在text中第一次出现的位置)

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

Problem A Number Sequence(KMP基础)

A - Number Sequence Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1711 Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 100