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 ...
随机推荐
- JAVA中文字符串编码--GBK转UTF-8
转载自:https://www.cnblogs.com/yoyotl/p/5979200.html 一.乱码的原因 gbk的中文编码是一个汉字用[2]个字节表示,例如汉字“内部”的gbk编码16进制的 ...
- js替换函数用法
定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法 stringObject.replace(regexp/substr,replac ...
- Applied Nonparametric Statistics-lec9
Ref:https://onlinecourses.science.psu.edu/stat464/print/book/export/html/12 前面我们考虑的情况是:response是连续的, ...
- leetcode-24-exercise
506. Relative Ranks 解题思路: 使用priority_queue.它在插入时会将数据按照由大到小的顺序插入,自然排序了.所以插入时考虑插入pair<nums[i],i> ...
- 最长公共子序列(LCS)问题
最长公共子串(Longest Common Substirng)和最长公共子序列(Longest Common Subsequence,LCS)的区别为:子串是串的一个连续的部分,子序列则是从不改变序 ...
- HDU:2586-How far away
How far away Time limit1000 ms Memory limit32768 kB Problem Description There are n houses in the vi ...
- Persona5
65536K Persona5 is a famous video game. In the game, you are going to build relationship with your ...
- 打印机增强软件pdfpro
http://3dx.pc6.com/gm1/pdfpro.zip
- while True 死循环
while True 死循环示例: count = 0 #给count设置变量为0 while True: count += 1 #每循环一次,count+1 : count += 1 等同于coun ...
- P3369 【模板】普通平衡树FHQtreap
P3369 [模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询 ...