Number Sequence---hdu1711(kmp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711

题意就是求b数组在a数组出现的位置;就是kmp模板;

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

const int N = 1e6+7;

int a[N], b[N], Next[N], n, m;

void GetNext()
{
    int i=0, j=-1;
    Next[0] = -1;
    while(i<n)
    {
        if(j==-1 || b[i] == b[j])
            Next[++i] = ++j;
        else
            j=Next[j];
    }
}
int kmp()
{
    int i=0, j=0;
    while(i<m)
    {
        if(j==-1 || a[i] == b[j])
        {
            i++;j++;
        }
        else
            j=Next[j];
        if(j==n)///如果j==n就返回下标;
            return i-n+1;
    }
    return -1;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &m, &n);
        for(int i=0; i<m; i++)
            scanf("%d", &a[i]);
        for(int i=0; i<n; i++)
            scanf("%d", &b[i]);
        GetNext();
        int ans = kmp();
        printf("%d\n", ans);
    }
    return 0;
}

时间: 2024-09-30 19:02:10

Number Sequence---hdu1711(kmp)的相关文章

HDU 5918 Sequence I (KMP)

Sequence I Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2330    Accepted Submission(s): 874 Problem Description Mr. Frog has two sequences a1,a2,?,an and b1,b2,?,bm and a number p. He wants t

【hdu 5918】Sequence I(KMP)

给定两个数字序列,求a序列中每隔p个构成的p+1个序列中共能匹配多少个b序列. 例如1 1 2 2 3 3 每隔1个的序列有两个1 2 3 kmp,匹配时每次主串往前p个,枚举1到p为起点. 题目 #include<bits/stdc++.h> #define N 1000005 int t,n,m,p; int nex[N]; int a[N],b[N]; using namespace std; void getNext(){ int i=0,k=-1; nex[0]=k; while(b

codeforces round#259 div2 B题(KMP)

先上链接:http://codeforces.com/contest/454/problem/B B. Little Pony and Sort by Shift time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day, Twilight Sparkle is interested in how to sort a se

HDU3336-Count the string(KMP)

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4449    Accepted Submission(s): 2094 Problem Description It is well known that AekdyCoin is good at string problems as well as n

poj1961 &amp; hdu 1358 Period(KMP)

poj 题目链接:http://poj.org/problem?id=1961 hdu题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 Description For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether

CQU 单词替换(KMP)

单词替换(KMP) Time Limit: 500 MS Memory Limit: 64000 K Description 给出一个仅包含小写字母的字符串s,和单词A,B.把s中所有的出现过的A替换为B. Input 第一行一个数T(1<=T<=10),表示数据组数 每组数据三行,第一行为s,第二行为A,第三行为B.所有字符串仅包含小写字母 且长度小于5,000,000. Output 每组数据输出一行,替换后的字符串. Sample Input 3 aaa a b aaa aa b aba

串的模式匹配算法(KMP)

算法: #include<IOSTREAM> using namespace std; #define MAXSIZE 100 void calNext(const char *T,int *next);//T为模式串,next为预判数组 int kmp_match(const char *S,const char *T);//在主串S中寻找模式串T,如果找到返回其位置,否则返回-1.位置从0开始 void calNext(const char *T,int *next) { int n =

HDU 2594 Simpsons’ Hidden Talents (KMP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 这题直接用KMP算法就可以做出来,不过我还尝试了用扩展的kmp,这题用扩展的KMP效率没那么高. KMP算法: #include<stdio.h> #include<iostream> #include<string.h> using namespace std; int next[50001]; char p[50000],s[50000]; void getnex

hdu 3336 Count the string(KMP)

一道应用kmp算法中next数组的题目 这其中vis[i]从1加到n vis[i]=[next[i]]+1; #include<string.h> #include<stdlib.h> #include<stdio.h> #include<iostream> #include<algorithm> using namespace std; char s[200005]; int b; int next[200005]; int vis[20000

URAL 1306 Sequence Median(优先队列)

题意:求一串数字里的中位数.内存为1M.每个数范围是0到2的31次方-1. 思路:很容易想到把数字全部读入,然后排序,但是会超内存.用计数排序但是数又太大.由于我们只需要第n/2.n/2+1大(n为偶数)或第(n+1)/2大(n为奇数).所以可以用优先队列来维护最值,这样只需要存一半元素(n/2+1个元素)就可以了. #include<cstdio> #include<algorithm> #include<queue> #define UL unsigned int