zoukankan      html  css  js  c++  java
  • HDU 1711 kmp

    Number Sequence

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


    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
     题意:给你a b数组  在a串中定位b串 输出最小的匹配开始位置 没有则输出-1
     题解:kmp
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int f[2000100];
     4 void get(int *p,int m)
     5 {
     6     int j=0;
     7     f[0]=f[1]=0;
     8     for(int i=1;i<m;i++)
     9     {
    10         j=f[i];
    11         while(j&&p[j]!=p[i]) j=f[j];
    12         if(p[i]==p[j]) f[i+1]=j+1;
    13         else f[i+1]=0;
    14     }
    15 }
    16 int kmp(int *s,int *p,int n,int m)
    17 {
    18     int j=0;
    19     int i;
    20     for(i=0;i<n;i++)
    21     {
    22         while(j&&p[j]!=s[i]) j=f[j];
    23         if(s[i]==p[j]) j++;
    24         if(j==m)
    25             break;
    26     }
    27     if(j==m)
    28     return i-m+1;
    29     else
    30     return -1;
    31 }
    32 int p[20100],t[2000100];
    33 int n,m;
    34 int main()
    35 {
    36     int T;
    37     scanf("%d",&T);
    38     for(int k=1;k<=T;k++)
    39     {
    40         scanf("%d %d",&n,&m);
    41         memset(t,0,sizeof(t));
    42         memset(p,0,sizeof(p));
    43         for(int i=0;i<n;i++)
    44             scanf("%d",&t[i]);
    45         for(int i=0;i<m;i++)
    46             scanf("%d",&p[i]);
    47         get(p,m);
    48         int ans=0;
    49         ans+=kmp(t,p,n,m);
    50         if(ans==-1)
    51             printf("-1
    ");
    52         else
    53         printf("%d
    ",ans+1);
    54     }
    55     return 0;
    56 }
  • 相关阅读:
    学了这些,你就算入门DIV+CSS了(转)
    【转】Web service是什么?
    [转]MongoDB插入和查询操作
    【转】安装m2e最佳方案
    XDS框架基本事务及其Soap消息实例
    数据表改变之后数据的迁移
    HL7 PID (Patient Identification) Segment(HL7规定的PID字段)
    海量数据库的设计
    【转】基于Axis2开发WebService
    【转】JAVA 调用Web Service的方法
  • 原文地址:https://www.cnblogs.com/hsd-/p/6665498.html
Copyright © 2011-2022 走看看