https://blog.csdn.net/slimmm/article/details/83989811
KMP算法
准备搞压缩看他文章 不清楚干啥的 先放着这
#include <stdio.h>
#include <string.h>//memset
#include <stdlib.h>//free
#include <stddef.h>
#define uint8_t unsigned char
#define uint16_t unsigned short
#define uint32_t unsigned int
#define uint64_t unsigned long long
#define uLong unsigned long
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
#define TEST_STR 0 //选择测试对象是字符串还是数组
typedef struct KMP
{
uchar *src; //源数据
uchar *dst; //要求匹配的数据
ushort sLen; //源数据长度
ushort pLen; //匹配数据长度
ushort coord; //匹配到数据记录其坐标
ushort matchLen; //最大的匹配长度
}KMP_TypeDef;
KMP_TypeDef KMP;
bool KMP_Search(KMP_TypeDef *KMP)
{
int i=0,j=0,max=0,baki=0;
if(KMP->sLen==0 || KMP->pLen==0)
{
return false;
}
while(i < KMP->sLen)
{ //把源数据遍历一遍
if(KMP->src[i] == KMP->dst[j]){ //从源数据中找到与匹配数据相同的数据
i++;
j++;
if(j >= max)
{
max = j; //更新最长匹配数据长度
baki = i; //备份坐标值
}
}
else
{
i = i - j + 1;
j = 0; //如果失配,则下一次匹配从匹配数据的第一个数据开始
}
}
KMP->coord = baki-max; //找到最佳匹配数据的下标
KMP->matchLen = max; //匹配数据长度
return true;
}
int main(void)
{
uchar str[100]={0};
KMP_TypeDef kmp;
#if TEST_STR
kmp.src = "a b a c a a b a c a b a c a b a a b b";//"ABCDEFGHIJKLMNOPQRSTUVWXYZd abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdesfghijklmnopqrstuvwxyz";//
kmp.dst = "a b a c a b";//"abcdesfghiu";//
kmp.sLen = strlen(kmp.src);
kmp.pLen = strlen(kmp.dst);
printf("slen:%d - plen:%d\n",kmp.sLen,kmp.pLen);
#else
uchar arr1[] = {0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f};
uchar arr2[] = {0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b};
kmp.sLen = sizeof(arr1)/sizeof(arr1[0]);
kmp.pLen = sizeof(arr2)/sizeof(arr2[0]);
kmp.src = (uchar *)malloc(kmp.sLen * sizeof(arr1[0]));
kmp.dst = (uchar *)malloc(kmp.pLen * sizeof(arr2[0]));
memcpy(kmp.src, arr1, kmp.sLen);
memcpy(kmp.dst, arr2, kmp.pLen);
printf("slen:%d - plen:%d\n",kmp.sLen,kmp.pLen);
#endif
KMP_Search(&kmp);
printf("coord:%d - Len:%d\n",kmp.coord, kmp.matchLen); //打印下标和长度
strncpy((char *)str, (char *)kmp.dst, kmp.matchLen); //字符串拷贝,这里拷贝数据也没出错
printf("Match: %s\n",str); //打印字符串,测试数组也将打印成字符串,具体内容可以参照ASCII码表
}
来源:CSDN
作者:GKoSon
链接:https://blog.csdn.net/weixin_42381351/article/details/103460762