A

来源:互联网 发布:收钱软件哪个好 编辑:程序博客网 时间:2024/06/11 00:50


以下是几种类型题:


A.类型1:匹配子字符串在母串中第几个位置开始出现。

B.类型2:子串在母串中出现了几次(可以有重复的)。

eg:子串:AZA

母串:AZAZAZA

ans = 3;

C.类型3:母串中最多有几个子串。

D.类型4:需要再补几个字符能构成一个类似手链那样循环相同的。

eg:abca ->2

abcde->5

aaa->0

E.类型5:给出一字符串,找出由2个或2个以上相同的子字符串组成的前缀,输出前缀长度及其相同的子字符串数。

F.类型6:当前最小循环节中字符串的个数(不可以补字符串)。

G.类型7:求最小循环节有几个。



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
213 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 1 313 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 2 1
Sample Output
6-1



AC代码:


#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>int p,q;int pext[1000005] = {-5};int l[1000005],y[1000005];using namespace std;void get_next(int *y,int *pext){int k = -1;int j = 0;pext[j] = k;while(j<q){if((k==-1)||(y[j]==y[k]))//判断匹配成功的条件 {k++;j++;pext[j] = k;//匹配成功都往后移 }else{k = pext[k];//匹配不成功,看j串是第几个不成功,假设第n个不成功,即j回溯为pext[n-1],找n-1处//的pext值,即为k的下一次的值,然后下一次从k的值处开始匹配。 }}/*for(int i=0;i<q;i++){printf("pext[%d] = %d\n",i,pext[i]);}*/}int index_KMP(int *l,int *y,int pos){int i,j;i = pos, j = 0;while((i<p)&&(j<q)){if((j==-1)||(l[i]==y[j])){i++;j++;}else{j = pext[j];}}//printf("%d %d\n",i,q);if(q==j){return i-q+1;}else{return -1;}}int main(){int n;int index;scanf("%d",&n);while(n--){int pos = 0;scanf("%d %d",&p,&q);for(int i=0;i<p;i++){scanf("%d",&l[i]);}for(int i=0;i<q;i++){scanf("%d",&y[i]);}get_next(y,pext);index = index_KMP(l,y,pos);printf("%d\n",index);}} 


原创粉丝点击