KMP 算法的 C 语言实现

cpp 复制代码
# include <stdio.h>
# include <stdlib.h>
# include <string.h>


// 打印 KMP 匹配结果.
void ColorPrint(char *T, int *result, int result_size, int m) {
    int green_size = strlen("\x1b[31m");
    int reset_size = strlen("\x1b[0m");

    char *color_string = (char *)malloc((strlen(T) + (green_size + reset_size) * result_size + 1) * sizeof(char));
    char *temp = (char *)malloc((strlen(T) + (green_size + reset_size) * reset_size + 1) * sizeof(char));

    strcpy(color_string, T);

    for (int idx = 0; idx < result_size; idx++) {
        strcpy(temp, color_string + result[idx] + idx * (green_size + reset_size));
        color_string[result[idx] + idx * (green_size + reset_size)] = '\0';
        strcat(color_string, "\x1b[31m");
        strcat(color_string, temp);

        strcpy(temp, color_string + result[idx] + m + green_size + idx * (green_size + reset_size));
        color_string[result[idx] + m + green_size + idx * (green_size + reset_size)] = '\0';
        strcat(color_string, "\x1b[0m");
        strcat(color_string, temp);
    }

    printf("%s\n", color_string);

    free(temp);
    temp = NULL;
    free(color_string);
    color_string = NULL;
}


// 获得 next 数组.
int *NextArray(char *P, int m) {
    int *next = (int *)calloc(m, sizeof(int));
    int idx = 1;
    int prefix_length = 0;
    while (idx < m) {
        if (P[prefix_length] == P[idx]) {
            next[idx++] = ++prefix_length;
        } else {
            if (prefix_length == 0) {
                idx++;
            } else {
                prefix_length = next[prefix_length - 1];
            }
        }
    }
    return next;
}


// KMP 模式匹配算法.
void KMP(char *T, char *P) {
    int i = 0;
    int j = 0;
    int n = strlen(T);
    int m = strlen(P);
    int *next = NextArray(P, m);
    int *result = NULL;
    int result_size = 0;

    while (i < n) {
        if (T[i] == P[j]) {
            i++; j++;
        } else {
            if (j == 0) {
                i++;
            } else {
                j = next[j - 1];
            }
        }
        if (j == m) {
            result = (int *)realloc(result, (result_size + 1) * sizeof(int));
            result[result_size++] = i - j;
        }
    }

    ColorPrint(T, result, result_size, m);

    free(result);
    result = NULL;
    free(next);
    next = NULL;
}


int main(int argc, char *argv[], char *env[]) {
    # ifdef _WIN32
        system("chcp 65001");
        KMP("KMP 算法一直是一个比较难以理解的算法。", "算法");
    # elif __linux__ || __APPLE__
        KMP("A space Odyssey is not about the Odyssey", "s");
    # endif
    return 0;
}
相关推荐
多打代码21 分钟前
2025.09.05 用队列实现栈 & 有效的括号 & 删除字符串中的所有相邻重复项
python·算法
黑客思维者2 小时前
《我是如何用C语言写工控系统的漏洞和Bug》连载(1)内容大纲
c语言·bug·工控漏洞
j_xxx404_2 小时前
数据结构:栈和队列力扣算法题
c语言·数据结构·算法·leetcode·链表
南莺莺2 小时前
假设一个算术表达式中包含圆括号、方括号和花括号3种类型的括号,编写一个算法来判别,表达式中的括号是否配对,以字符“\0“作为算术表达式的结束符
c语言·数据结构·算法·
THMAIL2 小时前
深度学习从入门到精通 - 神经网络核心原理:从生物神经元到数学模型蜕变
人工智能·python·深度学习·神经网络·算法·机器学习·逻辑回归
野犬寒鸦2 小时前
力扣hot100:旋转图像(48)(详细图解以及核心思路剖析)
java·数据结构·后端·算法·leetcode
墨染点香2 小时前
LeetCode 刷题【61. 旋转链表】
算法·leetcode·职场和发展
一枝小雨3 小时前
【OJ】C++ vector类OJ题
数据结构·c++·算法·leetcode·oj题
Tisfy3 小时前
LeetCode 3516.找到最近的人:计算绝对值大小
数学·算法·leetcode·题解
自信的小螺丝钉3 小时前
Leetcode 206. 反转链表 迭代/递归
算法·leetcode·链表