AC日记——Number Sequence hdu 1711
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24928 Accepted Submission(s):
10551
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.
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].
only contain K described above. If no such K exists, output -1
instead.
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
-1
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std; const int N = ;
int nest[],n,m;
int s[],t[];
int slen,tlen; void getnest()
{
memset(nest,,sizeof(nest));
int j,k;
j=;k=-;nest[]=-;
while(j<tlen)
{
if(k==-||t[j]==t[k])
nest[++j]=++k;
else k=nest[k];
}
} int kmp_index()
{
int i=;int j=;
getnest();
while(i<slen&&j<tlen)
{
if(j==-||s[i]==t[j])
{
i++;j++;
}
else j=nest[j];
}
if(j==tlen) return i-tlen+;
else return -;
} int kmp_count()
{
int ans=;
int i,j=;
if(slen==&&tlen==)
{
if(s[]==t[]) return ;
else return ;
}
getnest();
for(i=;i<slen;i++)
{
while(j>&&s[i]!=t[j])
{
j=nest[j];
}
if(s[i]==t[j]) j++;
if(j==tlen)
{
ans++;
j=nest[j];
}
}
return ans;
} int main()
{
int TT;
int i,cc;
cin>>TT;
while(TT--)
{
cin>>n>>m;
for(int i=;i<n;i++)
cin>>s[i];
for(int j=;j<m;j++)
cin>>t[j];
tlen=m,slen=n;
cout<<kmp_index()<<endl;
}
return ;
}
AC日记——Number Sequence hdu 1711的更多相关文章
- (KMP 模板)Number Sequence -- Hdu -- 1711
http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Other ...
- Number Sequence HDU 1711(KMP)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 首次接触KMP,自己都不是特别理解.在网上百度看了好几个帖子之后,对KMP也有了初步的理解. #inclu ...
- Number Sequence - HDU 1711(KMP模板题)
题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1 分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...
- Number Sequence HDU 1711 KMP 模板
题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #i ...
- Number Sequence ----HDOJ 1711
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Number Sequence(HDU 1005 构造矩阵 )
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Number Sequence HDU - 5014
There is a special number sequence which has n+1 integers. For each number in sequence, we have two ...
- AC日记——Keywords Search hdu 2222
2222 思路: ac自动机模板题: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...
- AC日记——Super Mario hdu 4417
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- Oracle 数据库常用SQL语句(2)查询语句
一.SQL基础查询 1.select语句 格式:select 字段 from 表名; 2.where 用于限制查询的结果. 3.查询条件 > < >= <= = != 4.与 ...
- Python中关于函数的介绍
一.什么是函数 当我们在日常工作中编写代码时,有没有发现这种情况,写了一套代码,却发现里面有很多段代码出现了有规律的重复,这样就不符合一个合格程序员的标准了,一个合格的程序员编写的代码最重 ...
- __vet_atags
参考:atags--__vet_atags标签 arch/arm/include/asm/setup.h /* * linux/include/asm/setup.h * * Copyright ...
- LeetCode(201) Bitwise AND of Numbers Range
题目 Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numb ...
- POJ:3041-Asteroids(匈牙利算法模板)
传送门:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Description Bes ...
- P1627 中位数
P1627 中位数 题目描述 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. 输入输出格式 输入格式: 第一行为两个正整 ...
- hdu1787 GCD Again poj 2478 Farey Sequence 欧拉函数
hdu1787,直接求欧拉函数 #include <iostream> #include <cstdio> using namespace std; int n; int ph ...
- 模拟 - BZOJ 1510 [POI2006] Kra-The Disks
BZOJ 1510 [POI2006] Kra-The Disks 描述 Johnny 在生日时收到了一件特殊的礼物,这件礼物由一个奇形怪状的管子和一些盘子组成. 这个管子是由许多不同直径的圆筒(直径 ...
- python学习-- class 类中需要注意的地方
from django.db import models class Person(models.Model): name = models.CharField(max_length=30) ...
- 2016-2017 ACM Central Region of Russia Quarterfinal Programming Contest BHanoi tower
B Hanoi tower It has become a good tradition to solve the “Hanoi tower” puzzle at programming contes ...