Number Sequence - HDU 1711(KMP模板题)

题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1

分析:应该是最简单的模板题了吧.....

代码如下:

==============================================================================================

#include<stdio.h>
#include<string.h>

const int MAXM = 1e4+7;
const int MAXN = 1e6+7;

int a[MAXN], b[MAXM], next_b[MAXM];

void GetNext(int b[], int next[], int M)
{
    int i=0, j=-1;
    next[0] = -1;

    while(i < M)
    {
        if(j==-1 || b[i]==b[j])
            next[++i] = ++j;
        else
            j = next[j];
    }
}
int KMP(int a[], int b[], int next[], int N, int M)
{
    int i = 0, j = 0;

    while(i < N)
    {
        while(j==-1 || a[i] == b[j] && j<M)
            i++, j++;

        if(j == M)return i-M+1;

        j = next[j];
    }

    return -1;
}

int main()
{
    int T;

    scanf("%d", &T);

    while(T--)
    {
        int i, N, M;

        scanf("%d%d", &N, &M);

        for(i=0; i<N; i++)
            scanf("%d", &a[i]);
        for(i=0; i<M; i++)
            scanf("%d", &b[i]);

        GetNext(b, next_b, M);

        printf("%d\n", KMP(a, b, next_b, N, M));
    }

    return 0;
}
时间: 2024-10-29 10:45:47

Number Sequence - HDU 1711(KMP模板题)的相关文章

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

(模板题)Number Sequence -- Hdu -- 1711

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): 16080    Accepted Submission(s): 7100 Problem Description Given two sequences of n

Number Sequence HDU - 1711

Given two sequences of numbers : a11 , a22 , ...... , aNN , and b11 , b22 , ...... , bMM (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make aKK = b11 , aK+1K+1 = b22 , ...... , aK+M?1K+M?1 = bMM . If there are mor

HDU 2087 kmp模板题

s为主串 t为模板串 求t的nextt 加const #include<stdio.h> #include<string.h> #include<algorithm> #include<map> #include<math.h> #include<queue> using namespace std; char s[1005]; char t[1005]; int nextt[1005]; void makenext(const ch

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

poj 3461 Oulipo(KMP模板题)

题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23559   Accepted: 9437 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a

Saving Princess claire_(hdu 4308 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2305    Accepted Submission(s): 822 Problem Description Princess claire_ wa

hdu 2586 LCA模板题(离线算法)

http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B&quo