KMP模板:
void getnext()
{
int i=0,j=-1,len=strlen(mo);
while(i<len)
{
next[0]=-1;
next[1]=0;
if(j==-1||mo[j]==mo[i])
{
i++;
j++;
next[i]=j;
}
else
{
j=next[j];
}
}
}
int kmp()
{
int i=0,j=0,l1=strlen(str),l2=strlen(mo);
int ans=0;
while(i<l1)
{
if(j==-1||mo[j]==mo[i])
{
i++;
j++;
}
else
{
j=next[j];
}
if(j==l2)
{
ans++;
}
}
return ans;
}
1;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
代码:
模板题:
#include<stdio.h>
#include<string.h>
int next[10005],lena,lenb;
int a[1000005],b[10005];
void set_naxt()
{
int i=0,j=-1;
next[0]=-1;
while(i<lenb)
{
if(j==-1||b[i]b[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
int kmp()
{
int i=0,j=0;
set_naxt();
while(i<lena)
{
if(j-1||a[i]==b[j])
{
i++;
j++;
}
else
j=next[j];
if(j==lenb)
return i-j+1;
}
return -1;
}
int main()
{
int i,t;
scanf("%d",&t);
while(t--)
{
memset(next,0,sizeof(next));
scanf("%d%d",&lena,&lenb);
for(i=0; i<lena; i++)
scanf("%d",&a[i]);
for(i=0; i<lenb; i++)
scanf("%d",&b[i]);
printf("%d\n",kmp());
}
}
2: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 from the book:
Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…
Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T’s is not unusual. And they never use spaces.
So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {‘A’, ‘B’, ‘C’, …, ‘Z’} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.
Input
The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:
One line with the word W, a string over {‘A’, ‘B’, ‘C’, …, ‘Z’}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
One line with the text T, a string over {‘A’, ‘B’, ‘C’, …, ‘Z’}, with |W| ≤ |T| ≤ 1,000,000.
Output
For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.
Sample Input
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
Sample Output
1
3
0
代码:
求模板在原串的个数,可以重叠。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
int Next[maxn];
char str[maxn],mo[maxn];
void getNext()
{
int i=0,j=-1,len=strlen(mo);
while(i<len)
{
Next[0]=-1;
Next[1]=0;
if(j==-1||mo[j]==mo[i])
{
i++;
j++;
Next[i]=j;
}
else
{
j=Next[j];
}
}
}
int kmp()
{
int i=0,j=0,l1=strlen(str),l2=strlen(mo);
int ans=0;
while(i<l1)
{
if(j==-1||mo[j]==str[i])
{
i++;
j++;
}
else
{
j=Next[j];
}
if(j==l2)
{
ans++;
}
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",&mo,&str);
getNext();
printf("%d\n",kmp());
}
return 0;
}
3:一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?
Input
输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。
Output
输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。
Sample Input
abcde a3
aaaaaa aa
Sample Output
0
3
这个题和和上一题一样,唯一不同就是不能重叠,所以每次找到一个就把模板的指针变为0。斜体样式
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
char str1[1005];
char str2[1005];
int Next[1005];
int cnt=0;
void makeNext(){
int q,k;
int m=strlen(str2);
Next[0]=0;
for(q=1,k=0;q<m;q++){
while(k>0&&str2[q]!=str2[k])
k=Next[k-1];
if(str2[q]==str2[k])
k++;
Next[q]=k;
}
}
void kmp(){
int i,q;
int len1=strlen(str1);
int len2=strlen(str2);
makeNext();
for(i=0,q=0;i<len1;i++){
while(q>0&&str2[q]!=str1[i])
q=Next[q-1];
if(str2[q]==str1[i])
q++;
if(q==len2){cnt++;q=0;}
}
}
int main(){
while(scanf("%s",str1)!=EOF){
cnt=0;
if(str1[0]=='#')break;
scanf("%s",str2);
kmp();
cout<<cnt<<endl;
}
return 0;
}