Number Sequence

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

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
 

题解:此题就是如果匹配就输出开始匹配时的数组下标;next数组有两个含义:位置还有长度;

让求串2在串1中首次出现的位置;

代码:

  1. #include<stdio.h>
  2. const int MAXN=;
  3. int a[MAXN*],b[MAXN],len1,len2,next[MAXN];
  4. void getnext(){
  5. int i=,j=-;
  6. next[i]=j;
  7. while(i<len2){
  8. if(j==-||b[i]==b[j]){
  9. i++;j++;
  10. next[i]=j;
  11. }
  12. else j=next[j];
  13. }
  14. }
  15. int kmp(){
  16. getnext();
  17. int i=,j=;
  18. while(i<len1){
  19. if(j==-||a[i]==b[j]){
  20. i++;j++;
  21. if(j==len2)return i-j+;
  22. }
  23. else j=next[j];
  24. }
  25. return -;
  26. }
  27. int main(){
  28. int T;
  29. int N,M;
  30. scanf("%d",&T);
  31. while(T--){
  32. scanf("%d%d",&N,&M);
  33. for(int i=;i<N;i++)scanf("%d",&a[i]);
  34. for(int i=;i<M;i++)scanf("%d",&b[i]);
  35. len1=N;len2=M;
  36. printf("%d\n",kmp());
  37. }
  38. return ;
  39. }
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. using namespace std;
  7. typedef long long LL;
  8. #define mem(x,y) memset(x,y,sizeof(x))
  9. #define SI(x) scanf("%d",&x)
  10. #define PI(x) printf("%d",x)
  11. #define P_ printf(" ")
  12. const int INF=0x3f3f3f3f;
  13. const int MAXN=1000010;
  14. int p[MAXN];
  15. int N,M;
  16. int s[MAXN];
  17. int m[MAXN];
  18. void getp(){
  19. int i=0,j=-1;
  20. p[0]=-1;
  21. while(i<M){
  22. if(j==-1||s[i]==s[j]){
  23. i++;j++;
  24. p[i]=j;
  25. }
  26. else j=p[j];
  27. }
  28. }
  29.  
  30. void kmp(int& ans){
  31. getp();
  32. int j=0,i=0;
  33. while(i<N){
  34. if(j==-1||s[j]==m[i]){
  35. i++;j++;
  36. if(j==M){
  37. ans=i-j+1;
  38. return ;
  39. }
  40. }
  41. else j=p[j];
  42. }
  43. }
  44.  
  45. int main(){
  46. int T;
  47. SI(T);
  48. while(T--){
  49. SI(N);SI(M);
  50. for(int i=0;i<N;i++)SI(m[i]);
  51. for(int i=0;i<M;i++)SI(s[i]);
  52. int ans=0;
  53. kmp(ans);
  54. if(!ans)puts("-1");
  55. else printf("%d\n",ans);
  56. }
  57. return 0;
  58. }

  str函数超时:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. using namespace std;
  7. typedef long long LL;
  8. #define mem(x,y) memset(x,y,sizeof(x))
  9. #define SI(x) scanf("%d",&x)
  10. #define PI(x) printf("%d",x)
  11. #define P_ printf(" ")
  12. const int INF=0x3f3f3f3f;
  13. const int MAXN=1000010;
  14. char s1[MAXN],s2[MAXN];
  15. int N,M;
  16. /*
  17. int s[MAXN];
  18. int m[MAXN];
  19. void getp(){
  20. int i=0,j=-1;
  21. p[0]=-1;
  22. while(i<M){
  23. if(j==-1||s[i]==s[j]){
  24. i++;j++;
  25. p[i]=j;
  26. }
  27. else j=p[j];
  28. }
  29. }
  30.  
  31. void kmp(int& ans){
  32. getp();
  33. int j=0,i=0;
  34. while(i<N){
  35. if(j==-1||s[j]==m[i]){
  36. i++;j++;
  37. if(j==M){
  38. ans=i-j+1;
  39. return ;
  40. }
  41. }
  42. else j=p[j];
  43. }
  44. }
  45. */
  46. int main(){
  47. int T;
  48. SI(T);
  49. while(T--){
  50. SI(N);SI(M);
  51. int temp;
  52. for(int i=0;i<N;i++)SI(temp),s1[i]=temp+'0';
  53. for(int i=0;i<M;i++)SI(temp),s2[i]=temp+'0';
  54. s1[N]='\0';s2[M]='\0';
  55. int ans=0;
  56. ans=strstr(s1,s2)-s1;
  57. if(ans<0)puts("-1");
  58. else{
  59. printf("%d\n",ans+1);
  60. }
  61. }
  62. return 0;
  63. }

  

Number Sequence(kmp)的更多相关文章

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

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

  2. 1711 Number Sequence(kmp)

    Number Sequence Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) To ...

  3. HDU 1711:Number Sequence(KMP)

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

  4. HDU 1711 Number Sequence(kmp)

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

  5. HDU 1711 Number Sequence (KMP)

    白书说这个是MP,没有对f 数组优化过,所以说KMP有点不准确 #include <stdio.h> int a,b; int T[1000010],P[10010];//从0开始存 in ...

  6. HDU 1711 Number Sequence(数列)

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

  7. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  8. HDU1711 Number Sequence(KMP模板题)

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

  9. HDU 1711 Number Sequence (KMP简单题)

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

随机推荐

  1. java Socket 使用注意

    Socket s = new Socket(ia, port); BufferedOutputStream bufOut = new BufferedOutputStream(s.getOutputS ...

  2. Linux环境Nginx安装多版本PHP

    php5.4.44 所谓多版本多版本PHP就是php5.4和5.5以及其他版本在同一台服务器. 假如php5.5是主版本已经安装在/usr/local/php目录下,那么再安装其他版本的php再指定不 ...

  3. UGUI 锚点

    今天我们来学习下UGUI的锚点, 他是做什么的呢?  基本上就是用于界面布局. 1. 1个控件对应1个描点. 2. 描点分成四个小叶片,  每1个叶片 对应 控件四边框的角点 3. 不管屏幕如何放大缩 ...

  4. Python 单词字母顺序不变且所有倒排

    翻出google測试project师的一道题目: 设计一个函数,不论什么语言都能够,实现下面功能: 一个句子,将句子中的单词所有倒排过来,但单词的字母顺序不变.eg.  this is a real ...

  5. Javascript中正则表达式的全局匹配模式

    先看一道JavaScript题目,据说是国内某知名互联网企业的JavaScript笔试题,如果对正则的全局匹配模式不了解的话可能会对下面的输出结果感到疑惑. var str = "123#a ...

  6. css直接写出小三角

    在开发移动端项目时,总是遇到很多小三角,之前一直用图片,感觉好麻烦,今天尝试了直接用CSS写出小三角!先看看如何写出各种小三角! /*箭头向上*/ .arrow-up { ; ; border-lef ...

  7. 将已有项目导入Gitlab

    登陆GitLab,创建添加项目. 写入项目的基本信息,包括名称.描述.权限等等. cd existing_folder git init git remote add origin git@10.10 ...

  8. hdu2952Counting Sheep

    Problem Description A while ago I had trouble sleeping. I used to lie awake, staring at the ceiling, ...

  9. hbase 二级索引创建

    在单机上运行hbase 二级索引: import java.io.IOException; import java.util.HashMap; import java.util.Map; import ...

  10. Struts2 工作流程

    Struts2使用了WebWork的设计核心(XWork),在内部使用拦截器处理用户请求,从而允许用户业务逻辑控制器和ServletAPI分离.Struts2内部是一个MVC架构,Struts2 的核 ...