HDU 1711 Number Sequence【KMP】【模板題】【水題】(返回匹配到的第一個字母的位置)
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 29634 Accepted Submission(s): 12464
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 返回匹配到的第一個字母的位置
#include <iostream> #include <cmath> #include <algorithm> #include <cstdio> #include <stdlib.h> #include <string> #include <cstring> #include <map> #include <set> #include <queue> #include <stack> #define INF 0x3f3f3f3f #define ms(x,y) memset(x,y,sizeof(x)) using namespace std; typedef long long ll; const double pi = acos(-1.0); const int mod = 1e9 + 7; const int maxn = 1e5 + 10; int nextval[1000010]; int s[1000010], p[10010]; int slen, plen; //p為模式串 void getnext(int p[], int nextval[]) //樸素kmp,nextval[i]即為1~i-1的最長前後綴長度 { int len = plen; int i = 0, j = -1; nextval[0] = -1; while (i < len) { if (j == -1 || p[i] == p[j]) { nextval[++i] = ++j; } else j = nextval[j]; } } //在s中找p出現的位置 int KMP(int s[], int p[], int nextval[]) { getnext(p, nextval); int ans = 0; int i = 0; //s下標 int j = 0; //p下標 int s_len = slen; int p_len = plen; while (i < s_len && j < p_len) { if (j == -1 || s[i] == p[j]) { i++; j++; } else j = nextval[j]; if (j == p_len) { return i - j + 1; } } return -1; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int t; scanf("%d", &t); while (t--) { scanf("%d%d", &slen, &plen); for (int i = 0; i < slen; i++) scanf(" %d", &s[i]); for (int i = 0; i < plen ; i++) scanf(" %d", &p[i]); printf("%d\n", KMP(s, p, nextval)); } return 0; }
相關推薦
HDU 1711 Number Sequence【kmp】
show turn case put next() void 出現 回首 img Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1
HDU 1711 Number Sequence 【KMP應用 求成功匹配子串的最小下標】
bottom () i++ mis time program inf all limit 傳送門:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS
HDU 1711 Number Sequence【KMP】【模板題】【水題】(返回匹配到的第一個字母的位置)
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原始
情況 scanf n) 而不是 文件 -m scan stdlib.h cnblogs #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib
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)
題目 Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s
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 Sequence KMP
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 42790&
hdu 1711 Number Sequence(KMP)
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 1711 Number Sequence (KMP找子串第一次出現的位置)(基礎模板題)
題意:給出若干個樣例,每個樣例包括兩個字串,如果第二個字串是第一個字串的子串,則求他第一次出現的位置,不是子串的話輸出-1. 思路:kmp演算法的模板題 對nexts陣列如何構造解釋: 根據定義ne
HDU - 1711 Number Sequence
esc -- nbsp scan mes each describe for ase Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 &
HDU 1711 Number Sequence
div ace sync algorithm mes 足道 std int ipo 結題思路:KMP標準模板題,註意KMP有兩個版本,這裏的優劣在代碼中已體現 1 #include <iostream> 2 #include <cstring>
HDU 1005 Number Sequence【迴圈節(取模)】
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 158149 Ac
HDU oj 1711 Number Sequence(KMP入門)
KMP原理KMP演算法是一種改進的字串匹配演算法,由D.E.Knuth,J.H.Morris和V.R.Pratt同時發現,因此人們稱它為克努特——莫里斯——普拉特操作(簡稱KMP演算法)。KMP演算法的關鍵是利用匹配失敗後的資訊,儘量減少模式串與主串的匹配次數以達到快速匹配的
hdoj 1711 Number Sequence KMP模板題
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1711 題目source:hdoj 1711 Number Sequence KMP分類難度級別1 題目含義:兩個數字串,尋找第二個數字串和第一個串中數字 匹配時候第一次出現的數字位置
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)
【BZOJ3781、2038】莫隊算法2水題
bsp space har 情況 ros clu while 給定 print 【BZOJ3781】小B的詢問 題意:有一個序列,包含N個1~K之間的整數。他一共有M個詢問,每個詢問給定一個區間[L..R],求Sigma(c(i)^2)的值,其中i的值從1到K,其中c(i
HDU 1005 Number Sequence
spa scanf scan %d content ble 取余 trac printf 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 找周期。當f(n)函數值再次出現1。1的時候就是一個循環
HDU 4390 Number Sequence (容斥原理+組合計數)
osi freopen ret dsm algo .cn iterator push_back man HDU 4390 題意: 大概就是這樣。不翻譯了:
【PAT-一道看著很難的水題】L2-023. 圖著色問題
pre math urn 問題 png info scan 水題 image 水題!沒其他想說的,還以為可以搞點高大上的搜索呢!十五分鐘,暴力兩重循環就OK了! 代碼如下: #include<iostream> #include<stdio.h>