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

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std; typedef long long LL;
const int N=10005;
const LL II=100000000;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0); int next[N],tlen,wlen;
int text[1000009],word[N]; void getnext(int *p)
{
int j=0,k=-1;
next[0]=-1;
while(j<wlen)//len是p的长度
{
if(k==-1||p[j]==p[k])
{
j++; k++;
next[j]=k;
}
else
k=next[k];
}
} int kmp(int *text,int *word)//主串、模式串
{//返回从第几个开始出现模式串
int i=0,j=0,k=0;
while(i<tlen)
{
if(j==-1||text[i]==word[j])
{
i++;j++;
}
else
j=next[j];
if(j==wlen) return i-wlen+1;
}
return -1;
} int main()
{
int i,j,T;
cin>>T;
while(T--)
{
scanf("%d%d",&tlen,&wlen);
for(i=0;i<tlen;i++)
scanf("%d",&text[i]);
for(i=0;i<wlen;i++)
scanf("%d",&word[i]);
getnext(word);
int xx=kmp(text,word);
printf("%d\n",xx);
}
return 0;
}

HDU 1711 Number Sequence KMP的更多相关文章

  1. HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. hdu 1711 Number Sequence KMP 基础题

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. HDU 1711 Number Sequence (KMP 入门)

    Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...

  4. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  5. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

  6. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  7. HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...

  8. HDU 1711 Number Sequence(KMP)附带KMP的详解

    题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...

  9. HDU 1711 Number Sequence (KMP简单题)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. codeforces 631D. Messenger kmp

    题目链接 首先想到kmp, 和普通的不一样的是,中间部分严格相等, 头和尾的字符相等但是数量可以不相等. 所以应该把子串的头和尾先去掉,然后对剩下的部分进行kmp. 子串长度为1或2要特别讨论. 不要 ...

  2. 输入输出函数库stdio.h

    函数名 函数类型与形参类型 函数功能 函数返回值 clearerr void clearerr(fp) FILE * fp; 清除文件指针错误 无 close int close(fp) int fp ...

  3. Python2.7.3 学习——第一个程序 Hello Python World

    Hello World 每学一门语言开始的第一程序都是Hello World ,当然了Python也不例外,下面开始我们的Python第一个程序编写: 1,命令行: (1)打开终端,输入python, ...

  4. grunt api 文档

    Grunt docs Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. 安装 grunt-cli npm install grunt-cli -g 注 ...

  5. Uber在华从沸点到冰点 搞定这些才能继续走下去

    腾讯科技 蒋栩根 5月6日报道 五一前夕,本应坐等笑看小长假带来的喜人业绩的Uber被广州市工商.交委.公安部门的联合检查当头浇了一盆冷水,也被戴上了一顶“涉嫌非法运营”的帽子.而一天前,它才与广物汽 ...

  6. 【课程分享】ASP.NET MVC5&amp;微信公众平台整合开发实战(响应式布局、JQuery Mobile,Windows Azure、微信核心开发)

    对这个课程有兴趣的,能够联系我QQ2748165793 基础知识储备 ASP.NET MVC 5基础(6讲) 第一讲-初识ASP.NET MVC并搭建整合开发环境 第二讲-深入MVC开发模式 第三讲- ...

  7. uva 10548 - Find the Right Changes(拓展欧几里得)

    题目链接:uva 10548 - Find the Right Changes 题目大意:给定A,B,C,求x,y,使得xA+yB=C,求有多少种解. 解题思路:拓展欧几里得,保证x,y均大于等于0, ...

  8. 倒计时IE6+

    很简单的 下面是我为了做多个倒计时更改之后的 dome 下载链接   兼容 IE7以上 IE6没测试应该没问题 http://yunpan.cn/cf29rxmGKuMyJ  提取码 ca61

  9. c++ primer plus 习题答案(8)

    p475.2 //头文件: class Cd{ private: char *performers; char *label; int selections; double playtime; pub ...

  10. mysql 本机root密码忘记

    1.找到对应的my.conf,在mysqld节点添加:skip-grant-tables  2.重启mysql 即可无密登录 3.update user表中的密码后,去除skip-grant-tabl ...