hdu 1711Number Sequence (KMP入门,子串第一次出现的位置)
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15149 Accepted Submission(s): 6644Problem DescriptionGiven
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.InputThe
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].OutputFor each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.Sample Input213 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 1 313 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 2 1Sample Output6-1Source
题意:给两个串a,b;求b在a中首次出现的位置,若没有則输出-1;
#include<iostream>
#include<vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <math.h>
#include<algorithm>
#define ll long long
#define eps 1e-8
using namespace std; int nexts[];
int str[],b[]; void pre_nexts(int m)
{
memset(nexts,,sizeof(nexts));
int j = ,k = -;
nexts[] = -;
while(j < m)
{
if(k == - || b[j] == b[k]) nexts[++j] = ++k;
else k = nexts[k];
}
}
int KMP(int n,int m)
{
int i,j = ;
for(i = ; i < n; )
{
if(str[i] == b[j])
{
if(j == m-) return i - (m - )+;//匹配成功,返回起始坐标
i++,j++;
}
else
{
j = nexts[j];
if(j == -) { i++,j = ;}//否则重新匹配
}
}
return -;
}
int main(void)
{
int t,i,m,n;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
for(i = ; i < n; i++)
scanf("%d",&str[i]);
for(i = ; i < m; i++)
scanf("%d",&b[i]);
if(n < m) { printf("-1\n"); continue;}//细节
pre_nexts(m);
printf("%d\n",KMP(n,m));
}
return ;
}
hdu 1711Number Sequence (KMP入门,子串第一次出现的位置)的更多相关文章
- hdu 1711 Number Sequence(kmp找子串第一次出现的位置)
题意:裸kmp 思路:kmp模板 #include<iostream> #include<stdio.h> #include<string.h> using nam ...
- HDU 1711 Number Sequence (KMP 入门)
Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...
- hdu 1358 Period(KMP入门题)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- Number Sequence(KMP,判断子串 模板)
题意: 给两数组,求一个是否是另一个的子数组,若是返回匹配的首位置 分析: KMP 入门 //扫描字符串A,并更新可以匹配到B的什么位置. #include <map> #include ...
- HDU 1711Number Sequence【KMP模板题】
<题目链接> 题目大意: 意思是给出两个串,找出匹配串在模式串中的位置. 解题分析: KMP算法模板题. #include <cstdio> #include <cstr ...
- hdu 2087剪花布条 (KMP入门 子串出现的次数和子串个数)
剪花布条 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 1711 Number Sequence【kmp求子串首次出现的位置】
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= ...
- hdu 1711Number Sequence
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 数字KMP,原来还能这么用 #include<stdio.h> ],b[]; ]; ...
- hdu 1358 period KMP入门
Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...
随机推荐
- Netty SimpleChannelInboundHandler和ChannelInboundHandler区别
一般用netty来发送和接收数据都会继承SimpleChannelInboundHandler和ChannelInboundHandlerAdapter这两个抽象类,那么这两个到底有什么区别呢? 在客 ...
- springboot项目中进行并发测试
一 利用工具包: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- 从0开始学习ssh之搭建环境
ssh即struts+spring+Hibernate,从头开始学习这个框架. struts环境配置,首先在apps目录下找到struts2-blank-xxx.war这个文件,这是已经发布好的war ...
- Windows API 第20篇 SetVolumeMountPoint 设置卷挂载点参数错误
函数原型:BOOL SetVolumeMountPoint( IN LPCTSTR lpszVo ...
- jiba中文分词原理
中文分词就是将一个汉字序列分成一个一个单独的词. 现有的分词算法有三大类: 基于字符串匹配的分词:机械分词方法,它是按照一定的策略将待分析的字符串与一个充分大的机器词典中的词条进行匹配,若在词典中找到 ...
- ASCII、Unicode、UTF-8 字符串和编码
字符编码 我们已经讲过了,字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特 ...
- transform函数
C++学习[原创]transform函数的应用 transform(first,last,result,op);//first是容器的首迭代器,last为容器的末迭代器,result为存放结果的容器, ...
- C++和G++手工开栈的=_=
微软的编译器(C++) #pragma comment(linker, "/STACK:102400000,102400000") G++ << ; // 256MB ...
- MATLAB技巧—sort和sortrows函数
MATLAB技巧-sort和sortrows函数 1.sort函数 sort函数用于对数据进行排序,通过help sort命令,可以查找到sort函数的具体用法: Y = SORT(X,DIM,MOD ...
- 装配SpringBean(六)--配置文件加载方式
spring中的配置文件有两种: 以XML结尾的spring配置文件 以properties结尾的属性配置文件 在spring中有两种方式加载这两种文件: 通过注解+java配置的方式 通过XML的方 ...