• HDU Problem 1711 Number Sequence 【KMP】


    Number Sequence

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

    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   |   We have carefully selected several similar problems for you:  1358 3336 1686 3746 1251 
    #include <map>
    #include <set>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <iostream>
    #include <stack>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <cstdlib>
    //#include <bits/stdc++.h>
    //#define LOACL
    #define space " "
    using namespace std;
    typedef long long Long;
    //typedef __int64 Int;
    typedef pair<int, int> paii;
    const int INF = 0x3f3f3f3f;
    const double ESP = 1e-5;
    const double Pi = acos(-1.0);
    const int MOD = 1e9 + 5;
    const int MAXN = 10000 + 5;
    int str1[MAXN*100], str2[MAXN];
    int Next[MAXN];
    void make_next(int P[], int m) {
        int q, k = 0; Next[0] = 0;
        for (q = 1; q < m; q++) {
            while(k > 0 && P[q] != P[k]) k = Next[k-1];
            if (P[q] == P[k])  k++;
            Next[q] = k;
        }
    }
    int main() {
        int t, m, n;
        scanf("%d", &t);
        while (t--) {
            scanf("%d%d", &n, &m);
            for (int i = 0; i < n; i++) {
                scanf("%d", &str1[i]);
            }
            for (int i = 0; i < m; i++) {
                scanf("%d", &str2[i]);
            }
            int ans = -1, j = 0;
            make_next(str2, m);
            for (int i = 0; i < n; i++) {
                while (j && str1[i] != str2[j]) j = Next[j - 1];
                if (str1[i] == str2[j]) j++;
                if (j == m) {
                    ans = i + 2 - j;
                    break;
                }
            }
            printf("%d
    ", ans);
        }
        return 0;
    }



  • 相关阅读:
    Lvs+Keepalived+Mysql单点写入主主同步高可用方案
    【转贴】应用服务器内存泄露问题诊断一例
    Java的内存回收机制
    【转贴】两年内从零到每月十亿 PV 的发展来谈 Pinterest 的架构设计
    JavaScript模板引擎简介
    ETL随笔(一)zz
    看图说话:为什么大数据落地难?
    蚂蚁变大象:浅谈常规网站是如何从小变大的zz
    hadoop资料整理zz
    对REST的理解
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770796.html
Copyright © 2020-2023  润新知