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;
}
相关推荐
爱装代码的小瓶子1 小时前
数据结构之队列(C语言)
c语言·开发语言·数据结构
爱喝矿泉水的猛男2 小时前
非定长滑动窗口(持续更新)
算法·leetcode·职场和发展
YuTaoShao2 小时前
【LeetCode 热题 100】131. 分割回文串——回溯
java·算法·leetcode·深度优先
YouQian7723 小时前
Traffic Lights set的使用
算法
快乐飒男3 小时前
哈希表(c语言)
c语言·哈希算法·散列表
go54631584654 小时前
基于深度学习的食管癌右喉返神经旁淋巴结预测系统研究
图像处理·人工智能·深度学习·神经网络·算法
aramae4 小时前
大话数据结构之<队列>
c语言·开发语言·数据结构·算法
大锦终5 小时前
【算法】前缀和经典例题
算法·leetcode
想变成树袋熊5 小时前
【自用】NLP算法面经(6)
人工智能·算法·自然语言处理
cccc来财5 小时前
Java实现大根堆与小根堆详解
数据结构·算法·leetcode