• HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)


    Number Sequence

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


    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
    分析:KMP裸题,自己看吧,不会的看我博客详解!此题有道坑点就是读入不能用cin读入,很容易T!

    纯粹要看运气才会过QAQ

    优化以后:

     速度快了将近3.5s,scanf大法好啊

    下面给出AC代码:
     
      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 const int N=1000050;
      4 inline int read()
      5 {
      6     int x=0,f=1;
      7     char ch=getchar();
      8     while(ch<'0'||ch>'9')
      9     {
     10         if(ch=='-')
     11             f=-1;
     12         ch=getchar();
     13     }
     14     while(ch>='0'&&ch<='9')
     15     {
     16         x=x*10+ch-'0';
     17         ch=getchar();
     18     }
     19     return x*f;
     20 }
     21 int kmpnext[N];
     22 int s[N],t[N];///s为主串,t为模式串
     23 int slen,tlen;///slen为主串的长度,tlen为模式串的长度
     24 inline void getnext()
     25 {
     26     int i,j;
     27     j=kmpnext[0]=-1;
     28     i=0;
     29     while(i<tlen)
     30     {
     31         if(j==-1||t[i]==t[j])
     32         {
     33             kmpnext[++i]=++j;
     34         }
     35         else
     36         {
     37             j=kmpnext[j];
     38         }
     39     }
     40 }
     41 /*
     42 返回模式串T在主串S中首次出现的位置
     43 返回的位置是从0开始的。
     44 */
     45 inline int kmp_index()
     46 {
     47     int i=0,j=0;
     48     getnext();
     49     while(i<slen&&j<tlen)
     50     {
     51         if(j==-1||s[i]==t[j])
     52         {
     53             i++;
     54             j++;
     55         }
     56         else
     57             j=kmpnext[j];
     58     }
     59     if(j==tlen)
     60         return i-tlen;
     61     else
     62         return -1;
     63 }
     64 /*
     65 返回模式串在主串S中出现的次数
     66 */
     67 inline int kmp_count()
     68 {
     69     int ans=0;
     70     int i,j=0;
     71     if(slen==1&&tlen==1)
     72     {
     73         if(s[0]==t[0])
     74             return 1;
     75         else
     76             return 0;
     77     }
     78     getnext();
     79     for(i=0;i<slen;i++)
     80     {
     81         while(j>0&&s[i]!=t[j])
     82             j=kmpnext[j];
     83         if(s[i]==t[j])
     84             j++;
     85         if(j==tlen)
     86         {
     87             ans++;
     88             j=kmpnext[j];
     89         }
     90     }
     91     return ans;
     92 }
     93 int T;
     94 int main()
     95 {
     96     T=read();
     97     while(T--)
     98     {
     99         slen=read();
    100         tlen=read();
    101         for(int i=0;i<slen;i++)
    102             s[i]=read();
    103         for(int i=0;i<tlen;i++)
    104             t[i]=read();
    105         if(kmp_index()==-1)
    106             cout<<-1<<endl;
    107         else
    108             cout<<kmp_index()+1<<endl;
    109     }
    110     return 0;
    111 }
  • 相关阅读:
    VIJOS P1057盖房子 (动态规划)
    RQNOJ PID57 / 找啊找啊找GF
    RQNOJ PID302 / [NOIP2001]统计单词个数 (动态规划)
    hdu 3829 Cat VS Dog 最大独立集
    并查集 找k颗树使节点数最多
    在 Sublime Text 3 中配置编译和运行 Java 程序
    在 Sublime Text 3 中配置编译和运行 Java 程序
    StarUML license key
    StarUML license key
    测试对于list的sort与sorted的效率
  • 原文地址:https://www.cnblogs.com/ECJTUACM-873284962/p/7113202.html
Copyright © 2020-2023  润新知