当前位置: 首页 > news >正文

KMP 算法的 C 语言实现

 

# 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;
}

相关文章:

  • ROS2-话题学习
  • RabbitMQ高级特性--消息确认机制
  • [网络爬虫] 动态网页抓取 — Selenium 入门操作
  • 搞定python之一----开发环境配置
  • AtCoder Beginner Contest 396(ABCDEF)
  • 【LLM】大模型推理、微调显卡挑选一览表
  • 【论文解读】《LIMO: Less is More for Reasoning》
  • PHP的Workerman 和 Java 常驻内存的相似性
  • Java【网络原理】(3)网络编程续
  • 如何避免项目后期盲目加人赶工
  • 机试准备第13天
  • 知识蒸馏综述Knowledge Distillation: A Survey解读
  • 国产算力助力工业智能新范式
  • PyTorch 学习路线
  • 探秘 Netty 通信中的 SslHandler 类:保障网络通信安全的基石
  • 【MySQL】发展起源与核心架构组件详细介绍
  • STL之list的使用(超详解)
  • 【时间序列】Patch:到底是什么?
  • 【极光 Orbit•STC8A-8H】03. 小刀初试:点亮你的LED灯
  • 数字信号处理之信号功率谱计算welch方法(分段加窗平均周期图)、Bartlett方法(周期图)(Python)
  • 微商城网站建设市场/牛奶推广软文文章
  • 做一般的公司网站需要多少钱/怎样查询百度收录和排名情况
  • 新网域名搭建网站/奶茶店推广软文500字
  • 南昌微信网站开发公司/国内最开放的浏览器
  • behance是什么网站/厦门seo公司到1火星
  • 海口做网站优化/如何推广品牌知名度