• hdu 1711Number Sequence


    http://acm.hdu.edu.cn/showproblem.php?pid=1711

    Number Sequence

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


    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
    View Code
     1 #include<stdio.h>
     2 int F[1000010];
     3 int S[10010];
     4 int n;
     5 int m;
     6 int next[10010];
     7 void get_next()
     8 {
     9     int i=1;
    10     int j=0;
    11     next[1]=0;
    12     while(i<m)
    13     {
    14         if(j==0||S[i]==S[j])
    15         {
    16             i++;
    17             j++;
    18             next[i]=j;
    19         }
    20         else j=next[j];
    21     }
    22     
    23 }
    24 int kmp()
    25 {
    26     int i=1;
    27     int j=1;
    28     int flag=1;
    29     while(i<=n&&j<=m)
    30     {
    31         
    32         if(j==0||F[i]==S[j])
    33         {
    34             i++;
    35             j++;
    36         }
    37         else j=next[j];
    38         if(j>m)
    39         {
    40             flag=0;
    41             printf("%d\n",i-j+1);
    42             break;
    43         }
    44     }
    45     if(flag) printf("-1\n");
    46     
    47 }
    48 int main()
    49 {
    50     int t;
    51     int i,j;
    52     scanf("%d",&t);
    53     while(t--)
    54     {
    55         scanf("%d%d",&n,&m);
    56         for(i=1;i<=n;i++)
    57         scanf("%d",&F[i]);
    58         for(i=1;i<=m;i++)
    59         scanf("%d",&S[i]);
    60         get_next();
    61         kmp();
    62     }
    63 }
  • 相关阅读:
    【maven】maven源码打包
    【分布式事务】阿里fescar
    第五章 mybatis批量更新update
    第二十九章 springboot + zipkin + mysql
    第二十八章 springboot + zipkin(brave定制-AsyncHttpClient)
    第二十七章 springboot + zipkin(brave-okhttp实现)
    附8 zipkin
    第二十六章 hystrix-dashboard + turbine
    附7 turbine
    第二章 部署war包到tomcat
  • 原文地址:https://www.cnblogs.com/1114250779boke/p/2620578.html
Copyright © 2020-2023  润新知