zoukankan      html  css  js  c++  java
  • AC日记——Number Sequence hdu 1711

    Number Sequence

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


    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模板;
     
     
    来,上代码:
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<cstdio>
    using namespace std;
    
    const int N = 1000002;
    int nest[1000002],n,m;
    int s[1000002],t[1000002];
    int slen,tlen;
    
    void getnest()
    {
        memset(nest,0,sizeof(nest));
        int j,k;
        j=0;k=-1;nest[0]=-1;
        while(j<tlen)
        {
            if(k==-1||t[j]==t[k])
                nest[++j]=++k;
            else k=nest[k];
        }
    }
    
    int kmp_index()
    {
        int i=0;int j=0;
        getnest();
        while(i<slen&&j<tlen)
        {
            if(j==-1||s[i]==t[j])
            {
                i++;j++;
            }
            else j=nest[j];
        }
        if(j==tlen) return i-tlen+1;
        else return -1;
    }
    
    int kmp_count()
    {
        int ans=0;
        int i,j=0;
        if(slen==1&&tlen==1)
        {
            if(s[0]==t[0]) return 1;
            else return 0;
        }
        getnest();
        for(i=0;i<slen;i++)
        {
            while(j>0&&s[i]!=t[j])
            {
                j=nest[j];
            }
            if(s[i]==t[j]) j++;
            if(j==tlen)
            {
                ans++;
                j=nest[j];
            }
        }
        return ans;
    }
    
    int main()
    {
        int TT;
        int i,cc;
        cin>>TT;
        while(TT--)
        {
            cin>>n>>m;
            for(int i=0;i<n;i++)
                cin>>s[i];
            for(int j=0;j<m;j++)
                cin>>t[j];
            tlen=m,slen=n;
            cout<<kmp_index()<<endl;
        }
        return 0;
    }
  • 相关阅读:
    求约数的个数-牛客
    成绩排序 -- 牛客
    SpringBoot学习笔记4-整合Jdbc Template-Mybatis-多数据源-事务管理-JPA
    SpringBoot学习笔记3-自定义拦截器-全局异常处理-Freemarker-Thymeleaf-定时任务调度
    SpringBoot学习笔记2-日志管理-开发模式-web开发-FastJson
    SpringBoot学习笔记1-简介-全局配置文件-starter-profiles-自动配置原理
    将Ueditor文件上传至OSS
    nouveau :failed to create kernel chanel,-22
    教你怎么炼鸡肉
    教你怎么写猜年龄游戏
  • 原文地址:https://www.cnblogs.com/IUUUUUUUskyyy/p/6533157.html
Copyright © 2011-2022 走看看