HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 27028 Accepted Submission(s): 11408
two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2],
...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your
task is to find a number K which make a[K] = b[1], a[K + 1] = b[2],
...... , a[K + M - 1] = b[M]. If there are more than one K exist, output
the smallest one.
first line of input is a number T which indicate the number of cases.
Each case contains three lines. The first line is two numbers N and M (1
<= M <= 10000, 1 <= N <= 1000000). The second line contains
N integers which indicate a[1], a[2], ...... , a[N]. The third line
contains M integers which indicate b[1], b[2], ...... , b[M]. All
integers are in the range of [-1000000, 1000000].

纯粹要看运气才会过QAQ
优化以后:

速度快了将近3.5s,scanf大法好啊
#include <bits/stdc++.h>
using namespace std;
const int N=;
inline int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
int kmpnext[N];
int s[N],t[N];///s为主串,t为模式串
int slen,tlen;///slen为主串的长度,tlen为模式串的长度
inline void getnext()
{
int i,j;
j=kmpnext[]=-;
i=;
while(i<tlen)
{
if(j==-||t[i]==t[j])
{
kmpnext[++i]=++j;
}
else
{
j=kmpnext[j];
}
}
}
/*
返回模式串T在主串S中首次出现的位置
返回的位置是从0开始的。
*/
inline int kmp_index()
{
int i=,j=;
getnext();
while(i<slen&&j<tlen)
{
if(j==-||s[i]==t[j])
{
i++;
j++;
}
else
j=kmpnext[j];
}
if(j==tlen)
return i-tlen;
else
return -;
}
/*
返回模式串在主串S中出现的次数
*/
inline int kmp_count()
{
int ans=;
int i,j=;
if(slen==&&tlen==)
{
if(s[]==t[])
return ;
else
return ;
}
getnext();
for(i=;i<slen;i++)
{
while(j>&&s[i]!=t[j])
j=kmpnext[j];
if(s[i]==t[j])
j++;
if(j==tlen)
{
ans++;
j=kmpnext[j];
}
}
return ans;
}
int T;
int main()
{
T=read();
while(T--)
{
slen=read();
tlen=read();
for(int i=;i<slen;i++)
s[i]=read();
for(int i=;i<tlen;i++)
t[i]=read();
if(kmp_index()==-)
cout<<-<<endl;
else
cout<<kmp_index()+<<endl;
}
return ;
}
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 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
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1711 AC代码: #include <iostream> #include <cs ...
- 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) ...
随机推荐
- dispatch_sync和dispatch_async的区别
dispatch_sync 线程同步.dispatch_async线程异步 比如 //同步 dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE ...
- SQL Server 文章目录
SQL Server系列: 高可用方案 SQL Server Alwayson概念总结 SQL Server AlwaysOn搭建 SQL Server2016 Alwayson新增功能 SQL Se ...
- PHP-CGI进程占用过多CPU
一般情况下,PHP-CGI只在用户访问的时候会占用CPU资源,但是最近有同事反映,服务器上的的PHP-CGI进程占用了非常多的CPU,但是访问流量却非常少.这显然是一个不正常的现象,说有些地方存在故障 ...
- Java ArrayIndexOutOfBoundsException: Exception Hierarchy
- PHP与REDIS
安装 1.一定要搞懂自己php的版本,和环境,今天试一上午,就是因为X86,而我的php环境是X64. 2. 将下载的php_redis.dll和php_igbinary.dll放在php扩展目录中( ...
- [编织消息框架][netty源码分析]1分析切入点
在分析源码之前有几个疑问 1.BOSS线程如何转交给handle(业务)线程2.职业链在那个阶段执行3.socket accept 后转给上层对象是谁4.netty控流算法 另外要了解netty的对象 ...
- ubuntu14 搭建单机版hadoop2.6
1. 如果你的集群尚未安装所需软件,你得首先安装它们. 以Ubuntu Linux为例: $ sudo apt-get install ssh $ sudo apt-get install rsync ...
- 前端生成验证码图片utils
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncod ...
- 房上的猫:for循环,跳转语句与循环结构,跳转语句进阶
一.for循环 1.定义: for循环语句的主要作用是反复执行一段代码,直到满足一定条件为止 2.组成部分: (1)初始部分:设置循环的初始状态 (2)循环体:重复执行的代码 (3)迭代部分: ...
- MySQL ALTER TABLE: ALTER vs CHANGE vs MODIFY COLUMN
ALTER COLUMN 语法: ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT} 作用: 设置或删除列的默认值.该操作会直接修 ...