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;
}
相关推荐
未知陨落14 分钟前
LeetCode:67.寻找旋转排序数组中的最小值
数据结构·算法·leetcode
超级大只老咪16 分钟前
编程竞赛高频考点
java·c语言·开发语言·c++·python
Gu_yyqx17 分钟前
快速排序总结
数据结构·算法·排序算法
Haooog19 分钟前
111.二叉树的最小深度(二叉树算法题)
java·数据结构·算法·二叉树
地平线开发者29 分钟前
模型插入 NV12 预处理节点精度问题排查流程
算法·自动驾驶
我要学习别拦我~1 小时前
逻辑回归中的成本损失函数全解析:从数学推导到实际应用
算法·机器学习·逻辑回归
元亓亓亓1 小时前
LeetCode热题--200. 岛屿数量--中等
算法·leetcode·职场和发展
学海一叶2 小时前
Agent开发02-关键思想(ReAct、ReWOO、Reflexion、LLM Compiler等)
人工智能·算法·llm·agent·plan
Learn Beyond Limits2 小时前
Initializing K-means|初始化K-means
人工智能·python·算法·机器学习·ai·kmeans·吴恩达
我想吃余2 小时前
【0基础学算法】前缀和刷题日志(一):前缀与后缀的交织
算法·leetcode