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;
}
相关推荐
jingfeng5143 分钟前
C++ STL-string类底层实现
前端·c++·算法
谱写秋天1 小时前
在STM32F103上进行FreeRTOS移植和配置(STM32CubeIDE)
c语言·stm32·单片机·freertos
我不是板神1 小时前
程序设计|C语言教学——C语言基础2:计算与控制语句
c语言
基于python的毕设1 小时前
C语言栈的实现
linux·c语言·ubuntu
雲墨款哥1 小时前
JS算法练习-Day10-判断单调数列
前端·javascript·算法
FPGA1 小时前
CRC校验原理及其FPGA实现
算法
Jina AI1 小时前
回归C++: 在GGUF上构建高效的向量模型
人工智能·算法·机器学习·数据挖掘·回归
Coovally AI模型快速验证1 小时前
YOLO、DarkNet和深度学习如何让自动驾驶看得清?
深度学习·算法·yolo·cnn·自动驾驶·transformer·无人机
luoqice1 小时前
linux下找到指定目录下最新日期log文件
linux·算法
楽码2 小时前
底层技术SwissTable的实现对比
数据结构·后端·算法