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) ...
随机推荐
- 挑战App Store,微信通过“跳一跳”秀了一下“小程序”的肌肉
2017年即将结束的时候,微信放了一个大招.随着最新的微信v6.6.1版本更新,基于小程序的"小游戏"板块正式上线.微信上首发的这款"小游戏"叫"跳一 ...
- [Count the numbers satisfying (m + sum(m) + sum(sum(m))) equals to N]
Given an integer N, the task is to find out the count of numbers M that satisfy the condition M + su ...
- P、NP、NP完全问题
如果一个算法的最差时间效率属于O(p(n)),则该算法可以在多项式的时间内对问题进行求解,其中p(n)是输入规模n的一个多项式函数. 可以在多项式时间内求解的问题是易解的.不能在多项式时间内求解的问题 ...
- Git 企业开发者教程
为什么要写这样一个面向企业开发者的Git教程?这个问题也困扰我自己很久.其实我使用git的时间也不短了,但是就和正在阅读本文的每一位一样,常用的基本就是那么几个(git clone, git pu ...
- Codebase Refactoring (with help from Go)
Codebase Refactoring (with help from Go) 代码库重构(借助于Go) 1.摘要 Go应该添加为类型创建替代等效名称的能力,以便在代码库重构期间渐进代码修复.本文解 ...
- 高度-宽度关系,同一div、不同div高度与宽度关系控制函数
//对象1的高度等于对象2的高度n倍,调用方法:Ht1DivideHt2('#div2','#div1',3)//div2的高度是div1高度的3倍function Ht1DivideHt2(obj1 ...
- mysql 编写存储过程
先看例子: 1.delimiter $$2.drop procedure if exists`test_procedure` $$3.create procedure test_procedure(I ...
- 使用图片地图减少HTTP请求数量
前言 最近在看<高性能网站建设>,记录一下所学. 现在很多网站都是图片形式的导航,点击图片跳转到对应的链接.如果导航项目很多的话,图片的数量就会很多,每需要加载一张图片就会多一个HTTP请 ...
- [总结]-2018 w1
不想总结 2017,过去的就过去吧,不过自己在 2017 年还是收获了很多,最重要的就是赚钱.赚钱还是需要两把刷子,所以,2018 的小目标就是学习数据分析和机器学习.希望自己在这两个领域能搞点事情. ...
- strace命令详解
转自: http://www.cnblogs.com/ahuo/p/4150623.html 备注: 这篇博文学到的不仅仅是 strace 这个命令,还有前辈的排错思路,致敬! strace 命令是一 ...