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) ...
随机推荐
- 5. 监视和ZooKeeper操作
ZooKeeper中的写入(write)操作是原子性和持久性的. 写入到大多数ZooKeeper服务器上的持久性存储中,可以保证写操作成功. 无论如何,ZooKeeper的最终一致性模型允许读取(re ...
- SSM :MyBatis与Spring的整合
MyBatis与Spring的整合 一:Spring整合MyBatis的准备工作: (1.)在项目中加入Spring,ByBatis及整合相关的jar文件 (2.)建立开发目录结构,创建实体类 (3. ...
- EAS(学生管理系统)初建
一.确定开发使用的技术 本次开发EAS示例网站,使用Servlet+JSP+MySQL技术,其中包括使用bootstrap工具完成简易前端页面设计.所有数据实体与数据关系皆用数 ...
- 很考验人的java内存加载面试题
源代码如下,求结果 public class MemoryAnalyse { public static int k = 0; public static MemoryAnalyse t1 = new ...
- MySQL GTIDs(global transaction identifiers)
1.如何定义和生成GTIDs 唯一性:在所有主从库都是唯一的,由二元组构成 每个事务和GTIDs之间都有1:1映射 GTID = source_id:transaction_id source_id标 ...
- Python之matplotlib模块安装
numpy 1.下载安装 源代码 http://sourceforge.net/projects/numpy/files/NumPy/ 安装 python2.7 setup.py install 2. ...
- Intrumentation类:ActivityInstrumentationTestCase2学习(1)
public abstract class ActivityInstrumentationTestCase2 extends ActivityTestCase//继承自ActivityTestCase ...
- MyBatis《1》
MyBatis入参考文档:http://mybatis.org/mybatis-3/zh/ 1.使用MyBatis前的准备 1.增加Maven依赖 <dependency> <g ...
- Java中常用加减密方式
1.加密概述: 加密就是是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使以获得了加密的信息,但因不知解密方式,仍无法了解信息的内容.大体上又分为双向加密和单向加密. 2.单项加密 2.1.概 ...
- ASP.NET异常处理机制
try{ //获取并使用资源,可能出现异常}catch(DivideByZeroException de){}catch(ArithmeticException ae){}catch(Exceptio ...