zoukankan      html  css  js  c++  java
  • HDU1711-Number Sequence

    Number Sequence

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


    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
     


    代码:

    #include<iostream>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<cstdlib>
    #include<cstdio>
    using namespace std;
    const int MAXN=1e6+2;
    int Next[MAXN];
    int t[MAXN],p[MAXN];
    int tlen,plen;
    void getNext() {
        int j,k;
        j=0;
        k=-1;
        Next[0]=-1;
        while(j<plen) {
            if(k==-1||p[j]==p[k]) {
                j++;
                k++;
                Next[j]=k;
            } else {
                k=Next[k];
            }
        }
    }
    int KMP_Index() {
        int i=0,j=0;
        getNext();
        while(i<tlen&&j<plen) {
            if(j==-1||t[i]==p[j]) {
                i++;
                j++;
            } else {
                j=Next[j];
            }
        }
        if(j==plen)
            return i-j+1;
        return -1;
    }
    int KMP_Count() {
        int ans=0;
        int i,j=0;
        if(tlen==1&&plen==1) {
            if(t[0]==p[0])
                return 1;
            return 0;
        }
        getNext();
        for(i=0; i<tlen; i++) {
            while(j>0&&t[i]!=p[j])
                j=Next[j];
            if(t[i]==p[j])
                j++;
            if(j==plen) {
                ans++;
                j=Next[j];
            }
        }
        return ans;
    }
    int main() {
        int tt;
        scanf("%d",&tt);
        while(tt--)
        {
            scanf("%d %d",&tlen,&plen);
            for(int i=0;i<tlen;i++)
            {
                scanf("%d",&t[i]);
            }
            for(int i=0;i<plen;i++)
            {
                scanf("%d",&p[i]);
            }
            printf("%d
    ",KMP_Index());
            //printf("%d
    ",KMP_Count());
        }
        return 0;
    }
    


  • 相关阅读:
    JDK和TOMCAT环境变量配置
    MEF(Managed Extensibility Framework )的入门介绍
    这样的数据导出你知道?
    ListBox实现拖拽排序功能
    各种技术资源汇总
    大话数据结构-排序
    大话数据结构-查找
    CheckListBox的实现方式分析
    listbox里面添加WrapPanel ,支持自适应换行
    RESTful 接口实现简明指南
  • 原文地址:https://www.cnblogs.com/lemonbiscuit/p/7776020.html
Copyright © 2011-2022 走看看