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;
}
相关推荐
多米Domi01142 分钟前
0x3f 第49天 面向实习的八股背诵第六天 过了一遍JVM的知识点,看了相关视频讲解JVM内存,垃圾清理,买了plus,稍微看了点确定一下方向
jvm·数据结构·python·算法·leetcode
_F_y7 小时前
MySQL用C/C++连接
c语言·c++·mysql
BackCatK Chen7 小时前
C语言学习栏目目录
c语言·保姆级教程·c语言入门·c语言学习栏目目录
极客数模9 小时前
【2026美赛赛题初步翻译F题】2026_ICM_Problem_F
大数据·c语言·python·数学建模·matlab
L_09079 小时前
【C++】高阶数据结构 -- 红黑树
数据结构·c++
A_nanda10 小时前
c# MOdbus rto读写串口,如何不相互影响
算法·c#·多线程
代码雕刻家11 小时前
2.4.蓝桥杯-分巧克力
算法·蓝桥杯
Ulyanov11 小时前
顶层设计——单脉冲雷达仿真器的灵魂蓝图
python·算法·pyside·仿真系统·单脉冲
请注意这个女生叫小美12 小时前
C语言 斐波那契而数列
c语言
Legendary_00812 小时前
Type-C 一拖二快充线:突破单口限制的技术逻辑
c语言·开发语言