zoukankan      html  css  js  c++  java
  • 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;
    }



  • 相关阅读:
    前端使用 node-gyp 构建 Native Addon
    CHANGELOG 的实现
    深入 JavaScript 中的对象以及继承原理
    使用electron进行原生应用的打包(2)---主进程与渲染进程之间的通信
    使用electron进行原生应用的打包
    Babel编译
    HTML布局四剑客-Flex,Grid,Table,Float
    关于vtt 与 srt 字幕 的相互转换
    关于websocket
    关于jQuery中nth-child和nth-of-type的详解
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770796.html
Copyright © 2011-2022 走看看