【KMP】Number Sequence

KMP算法

KMP的基处题目,数字数组的KMP算法应用。

主要是模式串的处理,当模式串内有重复时,模式串向左回溯重复的点的位置(next[])。

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

HDU 2007-Spring Programming Contest

 1 #include<stdio.h>
 2 int a[1000001],b[10001],next[10001];
 3 void getnext(int m){
 4     int i=1,j=0;
 5     next[1]=0;
 6     while(i<m){
 7         if(j==0||b[i]==b[j]){
 8             i++; j++; next[i]=j;
 9         }
10         else j=next[j];
11     }
12 }
13
14 void getk(int n,int m){
15     int i=1,j=1;
16     while(i<=n&&j<=m){
17         if(j==0||a[i]==b[j]){i++; j++;}
18         else j=next[j];
19     }
20     if(j>m) printf("%d\n",i-m);
21     else printf("-1\n");
22 }
23
24 int main()
25 {
26     int t,n,m,i,j;
27     scanf("%d",&t);
28     while(t--){
29         scanf("%d%d",&n,&m);
30         for(i=1;i<=n;i++) scanf("%d",&a[i]);
31         for(i=1;i<=m;i++) scanf("%d",&b[i]);
32         getnext(m);
33         getk(n,m);
34     }
35     return 0;
36 }

时间: 2024-10-27 11:15:19

【KMP】Number Sequence的相关文章

【poj1019】 Number Sequence

http://poj.org/problem?id=1019 (题目链接) 题意:给出一个数:1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910(当然中间是没有空格的)求它从左往右第n位上是多少. solution  水题一道.我们可以发现,这个数可以分成若干串数,记为i,那么每串数i就是从1~i.我们可以用数组x[i]来记录串i所占的空间也就是位数,数组sum[i]来记录串i的末尾位于整个串中的哪个位置.  对于每

【HDU 1005】Number Sequence —— 找周期

原题链接 Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 122213    Accepted Submission(s): 29653 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n

【POJ 1019】 Number Sequence

[POJ 1019] Number Sequence 二分水题 放组合数学里...可能有什么正规姿势吧Orz 112123123412345...这种串 分成长度1 2 3 4 5...的串 注意有多位数 把长度累加到一个数组里 注意要累加 因为查询的时候查的是原串中对应位置的数 因此要累加上前一次的长度 然后二分处该串前的总长 用查询的位置-之前串的总长 就是在最长的串中的位置 因此还要打个最长串的表 这些我都写一个循环里了 看着有点乱 可以拆开写... 代码如下: #include <ios

POJ3461 Oulipo 【KMP】

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22295   Accepted: 8905 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote

HDU3746 Cyclic Nacklace 【KMP】

Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2538    Accepted Submission(s): 1154 Problem Description CC always becomes very depressed at the end of this month, he has checke

HDU4763 Theme Section 【KMP】

Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1114    Accepted Submission(s): 579 Problem Description It's time for music! A lot of popular musicians are invited to join us in t

POJ2406 Power Strings 【KMP】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31388   Accepted: 13074 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

【BZOJ1367】[Baltic2004]sequence 左偏树

[BZOJ1367][Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sample Output 13 HINT 所求的Z序列为6,7,8,13,14,15,18.R=13 题解:详见论文 然而本题要求z[i]严格递增,那就让所有t[i]-=i就好了 #include <cstdio> #include <cstring> #include <iostrea

【KMP】【最小表示法】NCPC 2014 H clock pictures

题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个针都是相同的,分别指向Ai,Bi(360°被分成360000小份),问能否将其中一个旋转和另一个重合. 题目思路: [KMP][最小表示法] 循环同构问题.可以写KMP,我懒得写KMP了就写了循环同构的最小表示法. 首先将Ai排序,然后求差(记得取模360000,WA了一次),接下来复制一遍开始匹配. A