Number Sequence

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15017    Accepted Submission(s):
6585

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
#include<stdio.h>
#include<string.h>
#define MAX 10010
int n,m;
int b[MAX],a[MAX*100];
int next[MAX];
void getmap()
{
int i;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<m;i++)
scanf("%d",&b[i]);
}
void getfail()
{
int i,j;
next[0]=next[1]=0;
for(i=1;i<m;i++)
{
j=next[i];
while(j&&b[i]!=b[j])
j=next[j];
next[i+1]=b[i]==b[j]?j+1:0;
}
}
void kmp()
{
int i,j=0;
int ok=0;
for(i=0;i<n;i++)
{
while(j&&a[i]!=b[j])
j=next[j];
if(a[i]==b[j])
j++;
if(j==m)
{
ok=1;
printf("%d\n",i-m+2);//难点在这里
return ;
}
}
if(!ok)
printf("-1\n");
}
int main()
{
int i,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
getmap();
getfail();
kmp();
}
return 0;
}

  

hdoj 1711 Number Sequence【求字串在母串中第一次出现的位置】的更多相关文章

  1. Rabin_Karp(hash) HDOJ 1711 Number Sequence

    题目传送门 /* Rabin_Karp:虽说用KMP更好,但是RK算法好理解.简单说一下RK算法的原理:首先把模式串的哈希值算出来, 在文本串里不断更新模式串的长度的哈希值,若相等,则找到了,否则整个 ...

  2. HDOJ 1711 Number Sequence

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

  3. HDU 1711 Number Sequence(KMP匹配数字串)

    这个就是kmp的数组形式,不用来处理字符串还真有点不习惯呢... #include<iostream> using namespace std; ,MAXM = ; int T[MAXN] ...

  4. HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...

  5. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  6. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

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

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

  8. HDU 1711 Number Sequence(KMP)附带KMP的详解

    题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...

  9. HDU 1711 Number Sequence (KMP 入门)

    Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...

随机推荐

  1. cookie防篡改

    概述: 除了 session 外,一般不会在客户端的 cookies 里保存过于重要的凭据,但电商应用有时候不可避免地存储了一些敏感数据到客户端,当然不希望被篡改. 目的: 让服务器端能识别cooki ...

  2. Scanner中next()和nextline()读取字符串方法和区别

    在实现字符窗口的输入时,我个人更喜欢选择使用扫描器Scanner,它操作起来比较简单.在写作业的过程中,我发现用Scanner实现字符串的输入有两种方法,一种是next(),一种nextLine(), ...

  3. 浅谈KMP算法及其next[]数组

    KMP算法是众多优秀的模式串匹配算法中较早诞生的一个,也是相对最为人所知的一个. 算法实现简单,运行效率高,时间复杂度为O(n+m)(n和m分别为目标串和模式串的长度) 当字符串长度和字符集大小的比值 ...

  4. saiku

    1.saiku下载http://community.meteorite.bi/可以下载各个版本的源代码 2.下载到   saiku-latest.zip 3.解压运行比较简单     解压出来的目录: ...

  5. wpf样式绑定 行为绑定 事件关联 路由事件实例

    代码说明:我要实现一个这样的功能  有三个window窗口  每个窗体有一个label标签  当我修改三个label标签中任意一个字体颜色的时候  其他的label标签字体颜色也变化 首先三个窗体不用 ...

  6. TatukGIS - GisDefs - DateTimeToXMLString 函数

    函数名称  DateTimeToXMLString 所在单元  GisDefs 函数原型           function DateTimeToXMLString(_dtm: TDateTime; ...

  7. R语言数据分析

    CSDN博客:包括R语言基础.R语言数据挖掘.hadoop大数据及spark等 http://blog.csdn.net/qq_16365849 R语言及数据分析 http://blog.csdn.n ...

  8. js动态加载脚本

    最近公司的前端地图产品需要做一下模块划分,希望用户用到哪一块的功能再加载哪一块的模块,这样可以提高用户体验. 所以到处查资料研究js动态脚本的加载,不过真让人伤心啊!,网上几乎都是同一篇文章,4种方法 ...

  9. iscc2016-basic-明察秋毫

    查看源代码,找到maybe not flag : Jr1p0zr2VfPp 移位密码,注意判断字母大小写,并且数字无变化 s = "Jr1p0zr2VfPp" p = list(s ...

  10. Solr4.8.0源码分析(20)之SolrCloud的Recovery策略(一)

    Solr4.8.0源码分析(20)之SolrCloud的Recovery策略(一) 题记: 我们在使用SolrCloud中会经常发现会有备份的shard出现状态Recoverying,这就表明Solr ...