题目地址: 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. jq 22 一个很好图片显示

    Picbox 示例页面:http://www.jq22.com/Demo961

  2. 【转】CentOS图形界面的开启与关闭

    源自:http://blog.sina.com.cn/s/blog_4a1f76860100zpus.html 安装CentOS 5.6系统的时候我没有先装任何组件,现在用X Window,需要再安装 ...

  3. 字符串-06. IP地址转换

    /* * Main.c * D6-字符串-06. IP地址转换 * Created on: 2014年8月19日 *******测试通过******** *转载:http://blog.csdn.ne ...

  4. Spring-AOP实践

    Spring-AOP实践 公司的项目有的页面超级慢,20s以上,不知道用户会不会疯掉,于是老大说这个页面要性能优化.于是,首先就要搞清楚究竟是哪一步耗时太多. 我采用spring aop来统计各个阶段 ...

  5. Delphi2010新发现-类的构造和析构函数功能

    Delphi2010发布了. 虽然凭着对Delphi的热爱第一时间就安装了,但是现在可能是年纪大了,对新事物缺乏兴趣了.一直都没有仔细研究. 今天有点时间试了一下新功能. 本来C#和Delphi.NE ...

  6. cocos2dx 在mac下开发ios和android游戏

    这里主要说android,因为ios开发在文章 http://blog.csdn.net/itcastcpp/article/details/24792323 中已经说过. 1)打开工程 打开ecli ...

  7. SSO 基于Cookie+fliter实现单点登录(SSO):工作原理

    SSO的概念: 单点登录SSO(Single Sign-On)是身份管理中的一部分. SSO的一种较为通俗的定义是:SSO是指訪问同一server不同应用中的受保护资源的同一用户,仅仅须要登录一次,即 ...

  8. ostringstream的使用方法

    ostringstream的使用方法 [本文来自]http://www.builder.com.cn/2003/0304/83250.shtml http://www.cppblog.com/alan ...

  9. VS2013 RC 此模板尝试加载组件程序集 “NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral.........

    微软发布了vs2013的RC版本,更新了自己机器上的vs,在创建项目过程中,发现出现如题的相关错误,查了相关msdn的资料,才了解到vs已经全面切换到使用NuGet这个第三方开源工具来管理项目包和引用 ...

  10. JBoss 系列七十:一个简单的 CDI Web 应用

    概述 本文通过一个简单的 CDI Web 应用演示dependency injection, scope, qualifiers 以及EL整合.应用部署完成后我们可以通过http://localhos ...