我的第一道KMP。

把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题。

#include<stdio.h>
#include<string.h>
#define N 1000005
#define M 10005
int a[N],b[M];
int next[M];
int n,m;
void setNext()
{
int i,j;
i=0;
j=-1;
next[i]=j;
while(i<m)
{
if(j==-1||b[i]==b[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
return ;
}
int KMP()
{
int i,j;
i=j=0;
setNext();
while(i<n)
{
if(j==-1||a[i]==b[j])
{
i++;
j++;
if(j==m)
return i-m+1;
}
else
j=next[j];
}
return -1;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
int i;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<m;i++)
scanf("%d",&b[i]);
int temp;
temp=KMP();
printf("%d\n",temp);
}
return 0;
}

hdu 1711 Number Sequence(KMP模板题)的更多相关文章

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

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

  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 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. HDU 1711 Number Sequence (KMP 入门)

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

  5. HDU 1711 Number Sequence KMP

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1711 AC代码: #include <iostream> #include <cs ...

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

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

  7. HDU 1711 Number Sequence(数列)

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

  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应用 求成功匹配子串的最小下标】

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

随机推荐

  1. arclist标签和list标签区别

    很多站长朋友在刚入门织梦的时候对织梦的标签存在很多的困惑,关于arclist标签和list标签,甚至不知道啥时候用arclist,啥时用list标签.arclist 为自由列表,全局模板中都生效,一般 ...

  2. 破解tumblr背景音乐

    http://sex.tumblr.com/api/read/json?callback=streampadPlayerBlogV2.getAudioResponse&type=audio&a ...

  3. sersync做实时同步(第一步)

    两台主机,一台主服务器(192.168.0.109).一台目标服务器(192.168.0.212) 1.配置目标服务器(192.168.0.212);就是配置rsync服务器.在配置文件/etc/rs ...

  4. Struts2请求处理流程及源码分析

    1.1 Struts2请求处理 1. 一个请求在Struts2框架中的处理步骤: a) 客户端初始化一个指向Servlet容器的请求: b) 根据Web.xml配置,请求首先经过ActionConte ...

  5. 学习微信小程序之css17clearfix原理

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. HDU_2052——画矩形

    Problem Description Give you the width and height of the rectangle,darw it.   Input Input contains a ...

  7. sql语句相关操作

    create user test identified by test default tablespace users temporary tablespace temp quota 3M on u ...

  8. tomcat主目录

    简单显示天气预报js 代码

  9. iOS打电话、发邮件、发短信、打开浏览器

    //1.调用 自带mail [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://163@16 ...

  10. 创建Chromium WebUI接口

    转载自:http://www.chromium.org/developers/webui Chrome的WebUI是那种,在Chrome中输入 "chrome://xxxx"就能打 ...