Problem Description
Given 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.
 
Input
The 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].
 
Output
For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.
 
Sample Input
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
 
Sample Output
6
-1
非常久没有做kmp的题了,这几天要做一下。这是道模板题,直接套模板即可。
#include<stdio.h>
#include<string.h>
int s1[1000005],s2[10006],next[10006],n,m;
void nextt()
{
int i,j;
i=0;j=-1;
memset(next,-1,sizeof(next));
while(i<m){
if(j==-1 || s2[i]==s2[j]){
i++;j++;next[i]=j;
}
else j=next[j];
}
} int kmp()
{
int i,j;
i=0;j=0;
while(i<n && j<m){
if(j==-1 || s1[i]==s2[j]){
i++;j++;
}
else j=next[j];
}
if(j>=m){
return i+1-m;
}
else return 0;
} int main()
{
int i,j,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;i++){
scanf("%d",&s1[i]);
}
for(i=0;i<m;i++){
scanf("%d",&s2[i]);
}
nextt();
if(kmp()){
printf("%d\n",kmp());
}
else printf("-1\n");
}
return 0;
}

hdu1711 Number Sequence的更多相关文章

  1. HDU1711 Number Sequence(KMP模板题)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. HDU1711 Number Sequence KMP

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU1711 题意概括 给T组数据,每组有长度为n和m的母串和模式串.判断模式串是否是母串的子串,如果是输出 ...

  3. hdu1711 Number Sequence kmp应用

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目: Problem Description Given two sequences of n ...

  4. kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence

    Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...

  5. [裸KMP][HDU1711][Number Sequence]

    题意 找到子串在母串出现的第一个位置 解法 裸的KMP 特别的地方 第一次不看模板自己敲的KMP #include<stdio.h> const int maxn=100000; cons ...

  6. hdu1711 Number Sequence kmp模板

    题目传送门 学习博客 学习了kmp算法,理解了算法思想,但还没有到能把这个思想用语言来描述出来. #include<bits/stdc++.h> #define clr(a,b) mems ...

  7. HDU1711 Number Sequence 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目大意:最基础的字符串匹配,只不过这里用整数数组代替了字符串. 给你两个数组 \(a[1..N ...

  8. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

  9. HDU 1005 Number Sequence

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. CF 316C2(Tidying Up-二分图最大边权)

    C2. Tidying Up time limit per test 4 seconds memory limit per test 256 megabytes input standard inpu ...

  2. Oracle12C 怎样导入scott用户

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaG9uZ2thbmd3bA==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  3. hdu 1086(判断线段相交)

    传送门:You can Solve a Geometry Problem too 题意:给n条线段,判断相交的点数. 分析:判断线段相交模板题,快速排斥实验原理就是每条线段代表的向量和该线段的一个端点 ...

  4. TCP关闭过程

    状态迁移 . SO_LINGER/ SO_REUSEADDR TCP正常的关闭过程如下(四次握手过程): (FIN_WAIT_1) A ---FIN---> B(CLOSE_WAIT) (FIN ...

  5. hdu 5071 Chat(模拟)

    题目链接:hdu 5071 Chat 题目大意:模拟题. .. 注意最后说bye的时候仅仅要和讲过话的妹子说再见. 解题思路:用一个map记录每一个等级的妹子讲过多少话以及是否有这个等级的妹子.数组A ...

  6. URAL1523(dp+树状数组)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=41224#problem/B 分析:可以设dp[i][j]表示以i结尾长度为j的 ...

  7. coding 除了托管外,还能进行团队协作.

    coding 除了托管外,还能进行团队协作. oschina 也是非常不错的.

  8. python面向对象的继承

    无话可说,继承主要是一些父类继承,代码是非常具体的 #!/usr/bin/env python #coding:utf-8 class Father(object):#新式类 def __init__ ...

  9. java基金会成立Set

    1.设置 当向集合Set中添加对象时.首先集合计算要添加对象的hashcode,依据该值得到一个位置用来存放当前的对象,当在该位置没有一个对象存在的时候,集合set觉得该对象在集合中不存在,直接添加进 ...

  10. 在Button上、下、左、右位置加入图片和文字

    转载请注明出处:http://blog.csdn.net/droyon/article/details/37564419 非常多人有如标题所述的需求,并且大多数人採用了自己定义组件攻克了需求,事实上还 ...