1. 程式人生 > >1711(Number Sequence )(KMP)

1711(Number Sequence )(KMP)

Number Sequence

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


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

這是一道KMP演算法的模板題,KMP演算法在於NEXT陣列的理解,話不多說,記下模板吧。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[1000100];
int b[1000100];
int next_[1000010];
void get_next(int m)
{
    int i=0,j=-1;
    next_[0]=-1;
    while(i<m)
    {
        if(j==-1||b[i]==b[j])
        {
            next_[++i]=++j;
        }
        else
        {
            j=next_[j];
        }
    }
}
int kmp(int n,int m)
{
    int i=0,j=0;
    while(i<n)
    {
        if(j==-1||a[i]==b[j])
        {
            i++;
            j++;
            if(j==m)
                return i-m+1;
        }
        else
        {
            j=next_[j];
        }
    }
    return -1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        {
            for(int i=0;i<n;i++)
            {
                scanf("%d",&a[i]);
            }
            for(int i=0;i<m;i++)
            {
                scanf("%d",&b[i]);
            }
            get_next(m);
            int ans=kmp(n,m);
            printf("%d\n",ans);
        }
    }

}


相關推薦

1711Number Sequence KMP

Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 30288    Accep

51Nod - 1304 :字符串的相似度 裸的擴展KMP

字母 前綴 scan length clas str input printf while 我們定義2個字符串的相似度等於兩個串的相同前綴的長度。例如 "abc" 同 "abd" 的相似度為2,"aaa" 同 "aaab" 的相似度為3。 給出一個字符串S,計算S同他所有

【BZOJ1152】歌唱王國生成函數,KMP

導致 字符 com max 直接 std ++i problem 生成 【BZOJ1152】歌唱王國(生成函數,KMP) 題面 BZOJ 洛谷 題解 根據\(YMD\)論文來的QwQ。 首先大家都知道普通型生成函數是\(\displaystyle \sum_{i=0}^{\

字串學習總結Hash & Manacher & KMP

# 前言 終於開始學習新的東西了,總結一下字串的一些知識。 ## NO.1 字串雜湊(Hash) ### 定義 即將一個字串轉化成一個整數,並保證字串不同,得到的雜湊值不同,這樣就可以用來判斷一個該字串是否重複出現過。 所以說$Hash$就是用來求字串是否相同或者包含的。(包含關係就可以列舉區間,但是通常

HDU 1711 Number Sequence KMP簡單題

bmi AMM several rip case ive sts -- 沒有 Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe

HDU 1711 Number Sequence kmp

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

hdu 1711 Number SequenceKMP

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 <

HDU oj 1711 Number SequenceKMP入門

KMP原理KMP演算法是一種改進的字串匹配演算法,由D.E.Knuth,J.H.Morris和V.R.Pratt同時發現,因此人們稱它為克努特——莫里斯——普拉特操作(簡稱KMP演算法)。KMP演算法的關鍵是利用匹配失敗後的資訊,儘量減少模式串與主串的匹配次數以達到快速匹配的

HDU 1711 Number SequenceKMP】【模板題】【水題】返回匹配到的第一個字母的位置

Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 29634    Acce

HDU 1711 Number Sequence (KMP找子串第一次出現的位置基礎模板題

題意:給出若干個樣例,每個樣例包括兩個字串,如果第二個字串是第一個字串的子串,則求他第一次出現的位置,不是子串的話輸出-1. 思路:kmp演算法的模板題 對nexts陣列如何構造解釋: 根據定義ne

HDU 4390 Number Sequence 容斥原理+組合計數

osi freopen ret dsm algo .cn iterator push_back man HDU 4390 題意: 大概就是這樣。不翻譯了:

1005:Number Sequencehdu,數學規律題

his arch ear iostream tput ostream htm 數據 long Problem Description A number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (

[JZOJ 5437] [NOIP2017提高A組集訓10.31] Sequence 解題報告 KMP

題目連結: http://172.16.0.132/senior/#main/show/5437 題目: 題解: 發現滿足上述性質並且僅當A序列的子序列的差分序列與B序列的差分序列相同 於是我們把A變成差分序列,把B變成差分序列,做一次KMP就好了 #include<algorit

HDU-1005-Number Sequence 迴圈週期

原題連結: A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to

HDU 1005 Number Sequence矩陣乘法+快速冪

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.

HDU 1005 Number Sequence基礎矩陣快速冪

//HDU 1005 15MS 1424K #include <cstdio> #include <cstring> #include <cmath> #in

【HDOJ 1005】 Number Sequence 裸矩陣快速冪

原諒我貼個水題。。。攢了一年的'恩怨'終於瞭解了 b( ̄▽ ̄)d 去年就接觸過矩陣快速冪 線代太弱  看他們程式碼沒參悟透。。可能真是悟性太差了。。 然後一隻以為矩陣快速冪是很叼的東西(不過確實很叼) 太高深 再沒敢碰。。有毒啊………… 直到最近比賽(VJ)出現矩陣快速冪

1005 Number Sequence廣義斐波那契數列

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. G

1005 Number Sequence長得像矩陣快速冪的找規律

A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to ca

python百練1019:Number Sequence

題目: 描述 A single positive integer i is given. Write a program to find the digit located in the position i in the sequence of number grou