hdu 1711 Number Sequence

来源:互联网 发布:绘画软件价格 编辑:程序博客网 时间:2024/06/05 04:27
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711

题目大意:在母链中找到子链的位置,输出开始的位置。

#include <iostream>#include <cstdio>using namespace std;int lens,lenc,next[1000005],str[1000005],ch[1000005];int get_next(){    int i=0,j=-1;    next[0]=-1;    while (i<lens)    {        if (j==-1||str[i]==str[j])        {            i++;            j++;            next[i]=j;        }        else            j=next[j];    }}int kmp(){    int i=0,j=0;    get_next();    while (i<lenc)    {        if(j==-1||str[j]==ch[i])        {            i++;            j++;        }        else            j=next[j];        if (j==lens)            return i-j+1;    }    return -1;}int main (){    int t;    scanf("%d",&t);    while (t--)    {        //int n,m;        scanf("%d%d",&lenc,&lens);        for (int i=0; i<lenc; i++)            scanf("%d",&ch[i]);        for (int j=0; j<lens; j++)            scanf("%d",&str[j]);        printf ("%d\n",kmp());    }    return 0;}


0 0
原创粉丝点击