字元匹配-kmp
B站的KMP演算法講解:
視訊中的程式碼
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> using namespace std; void prefix_table(char pattern[], int prefix[], int n) { prefix[0] = 0; int len = 0; int i = 1; while(i < n) { if(pattern[i] == pattern[len]) { len++; prefix[i] = len; i++; } else { if(len > 0) { len = prefix[len-1]; } else { prefix[i] = len; i++; } } } } void move_prefix_table(int prefix[], int n) { for(int i = n-1; i > 0; i--) { prefix[i] = prefix[i-1]; } prefix[0] = -1; } void kmp(char text[], char pattern[]) { int n = strlen(pattern); int m = strlen(text); // int* prefix = malloc(sizeof(int) * n); //C語言可以用,c++用不了 int* prefix = (int*)malloc(sizeof(int)*n); //int *prefix = new int; prefix_table(pattern, prefix, n); move_prefix_table(prefix, n); //text[i] len(text) = m //pattern[j] len(pattern)=n int i = 0; int j = 0; while(i < m) { if(j == n - 1 && text[i] == pattern[j]) { printf("Find pattern at %d\n", i - j); j = prefix[j]; } if(text[i] == pattern[j]) { i++; j++; } else { j = prefix[j]; if(j == -1) { i++; j++; } } } } int main() { char pattern[] = "ABABCABAA"; char text[] = "ABABABCABAABABABAB"; kmp(text, pattern); return 0; }
暴力匹配法:
#include <iostream> #include <stdio.h> #include <string> #include <string.h> using namespace std; int main() { string m; string s; cout<<"請輸入主串:\n"; cin>>m; cout<<"請輸入子串:\n"; cin>>s; int cnt = 0; int flg; for (int j = 0; j <= (m.size() - s.size()); j++) { for (int i = 0; i < s.size(); i++) { flg = j; if (s[i] == m[j++]) { cnt++; } else { cnt = 0; i = 0; j = flg; break; } } } if (cnt == s.size()) { cout<<"匹配\n"; } else { cout<<"不匹配\n"; } return 0; }
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<bits/stdc++.h>
using namespace std;
const int N=1e6+13;
const int M=1e4+13;
int n,m;//n為長串長度,m為短串長度
int a[N];//長串
int b[M];//短串
int prefix[M];
void prefix_table(){
int i=0;
int j=1;
prefix[0]=0;
while(j<m){
if(b[j]==b[i]){
prefix[j]=++i;
j++;
}
else if(!i){
prefix[j]=0;//不知道為什麼不加這一句,打印出來也是0
j++;
}
else{
i=prefix[i-1];
}
}
// for(int j=0;j<m;j++) //prefix[]數組裡到底裝的是啥
// cout<<prefix[j]<<" ";
// cout<<endl;
}
int kmp(){
int i=0;
int j=0;
while(i<n&&j<m){
if(a[i]==b[j]){
i++;
j++;
}
else if(!j){
i++;
}
else{
j=prefix[j-1];
}
}
if(j==m)
return i-m+1;
else
return -1;
}
int main(){
int t;
cin>>t;
while(t--){
cin>>n>>m;
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<m;i++)
scanf("%d",&b[i]);
prefix_table();
cout<<kmp()<<endl;
}
return 0;
}
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 <iostream>
#include <string>
using namespace std;
string W;
string T;//T是主串
int prefix[10005];
void prefix_table() {
int i = 0;
int j = 1;
prefix[0] = 0;
while (j < W.size()) {
if (W[j] == W[i]) {
prefix[j] = ++i;
j++;
}
else if (!i) {
j++;
}
else {
i = prefix[i-1];
}
}
return ;
}
int kmp() {
int i = 0;
int j = 0;
int cnt = 0;
while (i < T.size()) {
if (T[i] == W[j]) {
i++;
j++;
}
else if (!j) {
i++;
}
else {
j = prefix[j-1];
}
if (j == W.size()) {
cnt++;
j = prefix[j-1];
}
}
return cnt;
}
int main() {
int n;
cin>>n;
while (n--) {
//先輸入子串,再輸入主串
cin>>W;
cin>>T;//主串
prefix_table();
cout<<kmp()<<endl;
}
return 0;
}
下面這個也超時,納悶
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
string a,b;//a是主串
int nxt[1005];
void getnxt() {
memset(nxt, 0, sizeof(nxt));
nxt[0] = -1;
int k = -1;
int i = 0;
int len = b.size();
while (i < len) {
if (k == -1 || b[i] == b[k]) {
i++;
k++;
nxt[i] = k;
}
else
k = nxt[k];
}
}
int kmp() {
int cnt = 0;
int i = 0;
int j = 0;
int lena = a.size();
int lenb = b.size();
while (i < lena) {
if (j == -1 || a[i] == b[j]) {
i++;
j++;
}
else
j = nxt[j];
if (j == lenb) {
cnt++;
j = 0;
i = i - lenb +1;
}
}
return cnt;
}
int main() {
int n;
cin>>n;
while (n--) {
cin>>b;
cin>>a;//主串
int len = b.size();
getnxt();
cout<<kmp()<<endl;
}
return 0;
}