串的模式匹配算法
在计算机科学中,串的模式匹配是一个重要的问题,涉及在一个主串(Text)中寻找一个子串(Pattern)的出现位置。模式匹配算法是解决这一问题的核心,主要目标是提高匹配效率。以下介绍几种常见的模式匹配算法及其实现。
1. 朴素匹配算法(Naive Matching Algorithm)
朴素匹配算法是最基本的字符串匹配算法,依次将模式串与主串的各个子串进行比较,直到找到匹配的子串或遍历完整个主串。其时间复杂度为O((n-m+1)*m),其中n为主串长度,m为模式串长度。
算法思路:
- 从主串的第一个字符开始,将模式串与主串的子串进行逐个字符比较。
- 如果匹配成功,则返回匹配的位置。
- 如果匹配失败,则将模式串右移一位,继续比较。
- 重复上述过程,直到找到匹配或遍历完整个主串。
代码实现:
#include <stdio.h>
#include <string.h>
// 朴素匹配算法
int naiveMatch(char* text, char* pattern) {
int n = strlen(text);
int m = strlen(pattern);
for (int i = 0; i <= n - m; i++) {
int j = 0;
while (j < m && text[i + j] == pattern[j]) {
j++;
}
if (j == m) {
return i; // 匹配成功,返回匹配位置
}
}
return -1; // 匹配失败
}
int main() {
char text[] = "hello world";
char pattern[] = "world";
int pos = naiveMatch(text, pattern);
if (pos != -1) {
printf("Pattern found at position %d\n", pos);
} else {
printf("Pattern not found\n");
}
return 0;
}
2. KMP算法(Knuth-Morris-Pratt Algorithm)
KMP算法通过预处理模式串,构建部分匹配表(Partial Match Table),避免了重复比较,显著提高了匹配效率。其时间复杂度为O(n + m)。
算法思路:
- 预处理模式串,构建部分匹配表(next数组),记录模式串中各个前缀的最长可匹配后缀长度。
- 利用部分匹配表,在匹配过程中遇到不匹配字符时,模式串右移位数为当前字符的部分匹配值,避免了重复比较。
代码实现:
#include <stdio.h>
#include <string.h>
// 计算部分匹配表(next数组)
void computeNext(char* pattern, int* next) {
int m = strlen(pattern);
next[0] = 0;
int j = 0;
for (int i = 1; i < m; i++) {
while (j > 0 && pattern[i] != pattern[j]) {
j = next[j - 1];
}
if (pattern[i] == pattern[j]) {
j++;
}
next[i] = j;
}
}
// KMP匹配算法
int kmpMatch(char* text, char* pattern) {
int n = strlen(text);
int m = strlen(pattern);
int next[m];
computeNext(pattern, next);
int j = 0;
for (int i = 0; i < n; i++) {
while (j > 0 && text[i] != pattern[j]) {
j = next[j - 1];
}
if (text[i] == pattern[j]) {
j++;
}
if (j == m) {
return i - m + 1; // 匹配成功,返回匹配位置
}
}
return -1; // 匹配失败
}
int main() {
char text[] = "hello world";
char pattern[] = "world";
int pos = kmpMatch(text, pattern);
if (pos != -1) {
printf("Pattern found at position %d\n", pos);
} else {
printf("Pattern not found\n");
}
return 0;
}
3. BM算法(Boyer-Moore Algorithm)
BM算法通过模式串的预处理,在匹配过程中利用坏字符规则(Bad Character Rule)和好后缀规则(Good Suffix Rule)实现大跨度的跳跃,从而提高匹配效率。其时间复杂度为O(n)。
算法思路:
- 预处理模式串,构建坏字符规则表和好后缀规则表。
- 从模式串的最后一个字符开始进行匹配,利用规则表决定模式串的移动位数。
代码实现:
#include <stdio.h>
#include <string.h>
#define ALPHABET_SIZE 256
// 计算坏字符表
void computeBadChar(char* pattern, int m, int badChar[ALPHABET_SIZE]) {
for (int i = 0; i < ALPHABET_SIZE; i++) {
badChar[i] = -1;
}
for (int i = 0; i < m; i++) {
badChar[(int)pattern[i]] = i;
}
}
// BM匹配算法
int bmMatch(char* text, char* pattern) {
int n = strlen(text);
int m = strlen(pattern);
int badChar[ALPHABET_SIZE];
computeBadChar(pattern, m, badChar);
int s = 0; // 模式串相对主串的偏移量
while (s <= n - m) {
int j = m - 1;
while (j >= 0 && pattern[j] == text[s + j]) {
j--;
}
if (j < 0) {
return s; // 匹配成功,返回匹配位置
} else {
s += (j - badChar[(int)text[s + j]] > 1) ? j - badChar[(int)text[s + j]] : 1;
}
}
return -1; // 匹配失败
}
int main() {
char text[] = "hello world";
char pattern[] = "world";
int pos = bmMatch(text, pattern);
if (pos != -1) {
printf("Pattern found at position %d\n", pos);
} else {
printf("Pattern not found\n");
}
return 0;
}
模式匹配算法在字符串处理和文本编辑中具有重要应用。朴素匹配算法简单易懂,但效率较低。KMP算法通过预处理模式串,避免重复比较,大幅提高了效率。BM算法则利用坏字符规则和好后缀规则,实现大跨度跳跃匹配,是效率较高的模式匹配算法。在实际应用中,根据需求选择合适的算法,可以显著提升程序的性能和处理速度。