Number Sequence

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 27028    Accepted Submission(s): 11408

Problem Description
Given
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.
 
Input
The
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].
 
Output
For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.
 
Sample Input
2
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 1 3
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 2 1
 
Sample Output
6
-1
 
Source
分析:KMP裸题,自己看吧,不会的看我博客详解!此题有道坑点就是读入不能用cin读入,很容易T!

纯粹要看运气才会过QAQ

优化以后:

速度快了将近3.5s,scanf大法好啊

下面给出AC代码:

 #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裸题,板子题,有坑点)的更多相关文章

  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 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...

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

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

  4. HDU 1711 Number Sequence KMP

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

  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. js登录滑动验证,不滑动无法登陆

    js的判断这里是根据滑块的位置进行判断,应该是用一个flag判断 <%@ page language="java" contentType="text/html; ...

  2. 【1】ArcGIS API for JavaScript 4.5/4.6 本地部署

    惭愧,和我的学弟比起来,我所开始接触前端开发,ArcGIS API for JavaScript的时间和深度远远不及于他. 一年之尾,亦是一年之始,我也将正式开始我的博客生涯.本人在校学习并且做项目, ...

  3. boost::format(字符串格式化库)

    这段时间学习boost库的使用,撰文一方面留以备用,另一方面就是shared精神. format主要是用来格式化std::string字符串以及配合std::cout代替C语言printf() 使用f ...

  4. ArcGIS 网络分析[8.4] 资料4 聚合——创建及打开网络数据集的类实现

    这篇是对前三篇的总结,因为网络数据集涉及的"点"太多了,我只能挑重点来设置,大家明白框架后可以自行寻求帮助文档添加功能. 我以C#类的形式给出,这个类包含很多种方法,因为本人的C# ...

  5. JS画几何图形之三【正弦曲线】

    数学式:y=Asin(ωx+φ)+k 样例:http://www.zhaojz.com.cn/demo/draw7.html 依赖:[点].[直线] JS函数的声明: //画正弦曲线 //dot 原点 ...

  6. whereis 命令详解

    whereis 作用:whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息.  参数:-b 定位可 ...

  7. JMeter IP欺骗压测

    要求:JMeter版本2.5以上 IP欺骗其实是LR自带的一个非常有用的功能. 为什么会用到IP欺骗? 1)当某个IP的访问过于频繁,或者访问量过大是,服务器会拒绝访问请求,这时候通过IP欺骗可以增加 ...

  8. Django内置Admin

    Django内置的Admin是对于model中对应的数据表进行增删改查提供的组件,使用方式有: 依赖APP: django.contrib.auth django.contrib.contenttyp ...

  9. mvc4.5更改为mvc4.0方法总结

    一:使用MVC4.5创建的项目结果IIS服务器不支持(windows server2008 支持.net4.0),整了半天终于有点眉目了,方法如下: 1.先将项目所在的文件夹找到,去掉文件夹及其文件的 ...

  10. Python函数返回不定数量的值

    Python的函数是可以return多个值的,但其本质上还是返回单个值,只是利用了tuple的自动打包,将多个值打包成单个tuple返回. 使用代码验证: def func_a(): return 1 ...