HDU 1711 Number Sequence KMP
题目地址: 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的更多相关文章
- HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1711 Number Sequence KMP 基础题
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 1711 Number Sequence (KMP 入门)
Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...
- HDU 1711 - Number Sequence - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- HDU 1711 Number Sequence (字符串匹配,KMP算法)
HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...
- HDU 1711 Number Sequence(KMP)附带KMP的详解
题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...
- HDU 1711 Number Sequence (KMP简单题)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- PHP中include和require的区别
include和require的区别,其实两者没有太大的区别,如果要包含的文件不存在,include提示notice,然后继续执行下面的语句,require提示致命错误并且退出. 根据测试,win32 ...
- justAP1.3.0版发布了
justAP是一个简单的.易于使用的php运行环境,适合php开发人员使用.与wamp.xampp等不同,它仅仅包含Apache httpd和Php,这也是它名字的来由(justAP=just Apa ...
- grunt api 文档
Grunt docs Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. 安装 grunt-cli npm install grunt-cli -g 注 ...
- PHP并发最佳实践
直接参考大牛的: http://www.searchtb.com/2012/06/rolling-curl-best-practices.html
- javascript简单对象创建
由于javascript中定义了一个函数就相当于定义了一个类,我们当然可以创建一个这个类的对象. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 ...
- 调用Android自带日历功能(日历列表单、添加一个日历事件)
调用Android自带日历功能 觉得这篇文章不错,转载过来. 转载:http://blog.csdn.net/djy1992/article/details/9948393 Android手机配备有 ...
- HDU 5893 List wants to travel(树链剖分)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5893 [题目大意] 给出一棵树,每条边上都有一个边权,现在有两个操作,操作一要求将x到y路径上所有边 ...
- javascript操作HTML元素
操作 HTML 元素 如需从 JavaScript 访问某个 HTML 元素,您可以使用 document.getElementById(id) 方法. 请使用 "id" 属性来标 ...
- 用css3画企鹅
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...
- 更改mysql 数据库名称
//创建新数据库 CREATE DATABASE hbwebTemporary; //移植每个表 RENAME TABLE hbweb.aircraft_info TO hbwebTemporary. ...