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 }
  • 相关阅读:
    pku 1330 LCA
    最近公共祖先(LCA)的Tarjan算法
    字典数模板
    字典树
    RMQ问题ST算法与模板
    RMQ(Range Minimum/Maximum Query)问题:
    罗穗骞的dc3和倍增法
    多重背包问题
    tarjan LCA 算法
    终极工程师的造诣 – 不迷信高科技的乔布斯给我们的启示
  • 原文地址:https://www.cnblogs.com/hsd-/p/6665498.html
Copyright © 2011-2022 走看看