HDU 1711 Number Sequence(字符串匹配)
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10571 Accepted Submission(s): 4814
= b[M]. If there are more than one K exist, output the smallest one.
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].
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
6
-1
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn1=1000010;
const int maxn2=10010;
int n,m;
int a[maxn1];
int b[maxn2];
int next[maxn2];
int main()
{
int kmp (int *a,int *b,int *next);
int t,i;
cin>>t;
while(t--)
{
cin>>n>>m;
for(i=0;i<n;i++)
{scanf("%d",&a[i]);}
for(i=0;i<m;i++)
{scanf("%d",&b[i]);}
int ans=kmp(a,b,next);
cout<<ans<<endl;
}
return 0;
}
//此函数用来求匹配串s串的next数组
void getnext (int *s,int *next)
{
next[0]=next[1]=0;
for (int i=1;i<m;i++)//m为匹配串s的长度
{
int j=next[i];
while (j&&s[i]!=s[j])
j=next[j];
next[i+1]=s[i]==s[j]?
j+1:0;
}
}
//此函数用来求匹配位置的值的函数。匹配不成功返回值-1
int kmp (int *a,int *b,int *next)
{
getnext (b,next);/////////
int j=0;
for (int i=0;i<n;i++)
{/////n为串1的长度
while (j&&a[i]!=b[j])
j=next[j];
if (a[i]==b[j])
j++;
if (j==m)//m为串2的长度
return i-m+2;
}
return -1;
}
HDU 1711 Number Sequence(字符串匹配)的更多相关文章
- 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) ...
- 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)
题目链接 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...
- KMP - HDU 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- AJAX 笔记
一.什么是 AJAX ? AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 是一种用于创建快速动态网页的技术. 通过 ...
- css 选择器和优先级
css样式是做网页时,页面 布局不可或缺的关键点.但是在做网页时,会遇到一些明明已经设置了样式的元素,缺无法达到想要的效果,这种情况比较常见.这就涉及到优先级的问题了 要说到css的优先级,先来看下c ...
- 【Win 10 应用开发】UI Composition 札记(五):灯光
UI Composition 除了能够为 UI 元素建立三维空间外,还有相当重要的一个部件——灯光.宇宙万物的精彩缤纷,皆源于光明,光,使我们看到各种东西,除了黑洞之外的世界都是五彩斑谰的.故而,真要 ...
- 《剑指offer》面试题的Python实现
<剑指Offer>是很多程序员面试前要看的书,但里面的算法都是基于C++实现的,最近用了三周左右时间,用Python完成了里面几乎所有的算法题,由于时间以及个人水平均有限,或许会有部分问题 ...
- 如何编写通用的 Helper Class
Github: https://github.com/nzbin/snack-helper Docs: https://nzbin.github.io/snack-helper 前言 什么是 help ...
- 分享一个单例模型类Singleton代码
相关代码: ; foreach (string key in dict.Keys) { if (cou ...
- mongoose返回值无法修改
mongoose 查询方法 find 例:db.collections.find(query,function(err,doc) { 如果var res = doc[0] 是{name:'feife ...
- Getting Started With setuptools and setup.py
https://pythonhosted.org/an_example_pypi_project/setuptools.html http://www.ianbicking.org/docs/setu ...
- 一致性hash算法以及其在分布式系统中的应用(转)
初始架构
- [转]如何查询SQL Server连接数
1.获取SQL Server允许同时用户连接的最大数 SELECT @@MAX_CONNECTIONS 2.获取当前指定数据库的连接信息 SELECT * FROM master.dbo.syspro ...