Number Sequence

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

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
 
Source

题意:

给出T和P求p出现的位置

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. const int maxn=;
  6. const int maxm=;
  7. int p[maxm],t[maxn];
  8. int f[maxm],n,m;
  9. void getfail(int* p,int* f){
  10. f[]=;f[]=;
  11. for(int i=;i<m;i++){
  12. int j=f[i];
  13. while(j&&p[i]!=p[j]) j=f[j];
  14. f[i+]=(p[i]==p[j]?j+:);
  15. }
  16. }
  17. int find(int* t,int* p,int* f){
  18. getfail(p,f);
  19. int j=;
  20. for(int i=;i<n;i++){
  21. while(j&&p[j]!=t[i]) j=f[j];
  22. if(p[j]==t[i]) j++;
  23. if(j==m) return i-m++;//下标从0开始
  24. }
  25. return -;
  26. }
  27. int main()
  28. {
  29. int cas;
  30. scanf("%d",&cas);
  31. while(cas--){
  32. scanf("%d%d",&n,&m);
  33. for(int i=;i<n;i++) scanf("%d",&t[i]);
  34. for(int i=;i<m;i++) scanf("%d",&p[i]);
  35. int ans=find(t,p,f);
  36. printf("%d\n",ans);
  37. }
  38. return ;
  39. }

HDU1711 KMP(模板题)的更多相关文章

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

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

  2. POJ Oulipo KMP 模板题

    http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4 ...

  3. POJ Oulipo(KMP模板题)

    题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...

  4. Number Sequence - HDU 1711(KMP模板题)

    题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1   分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...

  5. hdu 1711 Number Sequence(KMP模板题)

    我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...

  6. HDU 1711Number Sequence【KMP模板题】

    <题目链接> 题目大意: 意思是给出两个串,找出匹配串在模式串中的位置. 解题分析: KMP算法模板题. #include <cstdio> #include <cstr ...

  7. POJ:3461-Oulipo(KMP模板题)

    原题传送:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Description The F ...

  8. [HDU1711]KMP模板

    解题关键:1.直接套kmp模板即可,注意最后输出的位置,需要在索引的位置+1. 2.next用作数组名在oj中会编译错误, 3.选用g++,只有g++才会接受bits/stdc++.h OJ中g++和 ...

  9. (模板)poj3461(kmp模板题)

    题目链接:https://vjudge.net/problem/POJ-3461 题意:给出主串和模式串,求出模式串在主串中出现的次数. 思路:kmp板子题. AC代码: #include<cs ...

  10. HDU1711 Number Sequence(KMP模板题)

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

随机推荐

  1. python基本数据类型——元组

    元组 元组是一种不可变的序列,创建后不可以修改元素值 # 创建只包含一个元素的元组 >>a = (3,) >>print(a) (3,) #使用 tuple() 转换为元组 & ...

  2. adb 常用命令及操作

    获取序列号: adb get-serialno 查看连接计算机的设备: adb devices 重启机器: adb reboot 重启到bootloader,即刷机模式: adb reboot boo ...

  3. location 匹配规则 (NGINX)

    转:https://moonbingbing.gitbooks.io/openresty-best-practices/ngx/nginx_local_pcre.html location 匹配规则 ...

  4. 最短路径——floyd(多源最短路径)

    #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> ...

  5. 第十九次ScrumMeeting会议

    第十九次Scrum Meeting 时间:2017/12/9 地点:三公寓大厅 人员:蔡帜 王子铭 游心 解小锐 王辰昱 李金奇 杨森 陈鑫 赵晓宇 照片: 目前工作进展 名字 今日 明天的工作 蔡帜 ...

  6. NSTimer使用注意事项

    1.scheduled开头和非schedule的开头方法的区别.系统框架提供了几种创建NSTimer的方法,其中以scheduled开头的方法会自动把timer加入当前run loop,到了设定的时间 ...

  7. TCP系列26—重传—16、重组包

    一.介绍 在TCP重传的时候,并没有限制TCP只能重传与初传完全相同的报文段大小,TCP允许执行重组包(repacketization),发送一个更大的TCP报文段,进而增加性能.TCP在重传时候允许 ...

  8. <Effective C++>读书摘要--Resource Management<二>

    <Item 15> Provide access to raw resources in resource-managing classes 1.You need a way to con ...

  9. HDU 2133 What day is it

    http://acm.hdu.edu.cn/showproblem.php?pid=2133 Problem Description Today is Saturday, 17th Nov,2007. ...

  10. activity属性设置大全

    activity属性设置大全 android:allowTaskReparenting=["true" | "false"]         是否允许activ ...