zoukankan      html  css  js  c++  java
  • HDOJ1711 Number Sequence[KMP模版]

    Number Sequence

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


    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
     
    Recommend
    lcy
     
     
     
    题意:求完全匹配时匹配串的位置
     
    注意数组大小
     
    code:
     1 #include<iostream>
     2 using namespace std;
     3 
     4 #define MAXN  10000000
     5 
     6 int p[MAXN];
     7 int s[MAXN];
     8 int Next[MAXN];
     9 int n,m;
    10 
    11 void getNext()
    12 {
    13     int j,k;
    14     j=0;
    15     k=-1;
    16     Next[0]=-1;
    17     while(j<m)
    18     {
    19         if(k==-1||p[j]==p[k])
    20         {
    21             j++;
    22             k++;
    23             Next[j]=k;
    24         }
    25         else
    26             k=Next[k];
    27     }
    28 }
    29 
    30 int kmp()
    31 {
    32     int i,j;
    33     i=j=0;
    34     getNext();
    35     while(i<n)
    36     {
    37         if(j==-1||s[i]==p[j])
    38         {
    39             i++;
    40             j++;
    41         }
    42         else
    43         {
    44             j=Next[j];
    45         }
    46         if(j==m)
    47             return i;
    48     }
    49     return -1;
    50 }
    51 
    52 
    53 int main()
    54 {
    55     int t;
    56     int i;
    57     scanf("%d",&t);
    58     while(t--)
    59     {
    60         scanf("%d%d",&n,&m);
    61         for(i=0;i<n;i++)
    62             scanf("%d",&s[i]);
    63         for(i=0;i<m;i++)
    64             scanf("%d",&p[i]);
    65         if(kmp()==-1)
    66             printf("-1\n");
    67         else
    68             printf("%d\n",kmp()-m+1);
    69     }
    70     return 0;
    71 }
  • 相关阅读:
    Wireshark——工具
    Wireshark——网络协议
    Wireshark——过滤器
    Wireshark——数据包、着色规则和提示
    Floyd最小环
    有向环覆盖问题
    KM算法
    归并排序
    树状数组
    构造强连通图
  • 原文地址:https://www.cnblogs.com/XBWer/p/2645815.html
Copyright © 2011-2022 走看看