克努特 - 莫里斯 - 普拉特算法的C语言实现

克努特 - 莫里斯 - 普拉特算法的C语言实现,第1张

概述克努特 - 莫里斯 - 普拉特算法的C语言实现

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

/*   copyright 2011 Shao-Chuan Wang <shaochuan.wang AT gmail.com>     Permission is hereby granted,free of charge,to any person obtaining a copy    of this software and associated documentation files (the "Software"),to deal    in the Software without restriction,including without limitation the rights    to use,copy,modify,merge,publish,distribute,sublicense,and/or sell    copIEs of the Software,and to permit persons to whom the Software is    furnished to do so,subject to the following conditions:     The above copyright notice and this permission notice shall be included in    all copIEs or substantial portions of the Software.     THE SOFTWARE IS PROVIDED "AS IS",WITHOUT WARRANTY OF ANY KIND,EXPRESS OR    IMPLIED,INCLUDING BUT NOT liMITED TO THE WARRANTIES OF MERCHANTABIliTY,fitness FOR A PARTIculaR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE    AUTHORS OR copYRIGHT HolDERS BE liABLE FOR ANY CLaim,damAGES OR OTHER    liABIliTY,WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEAliNGS IN    THE SOFTWARE.*/#include <stdio.h>#include <stdlib.h>#include <string.h> int *compute_prefix_function(char *pattern,int psize){    int k = -1;    int i = 1;    int *pi = malloc(sizeof(int)*psize);    if (!pi)        return NulL;     pi[0] = k;    for (i = 1; i < psize; i++) {        while (k > -1 && pattern[k+1] != pattern[i])            k = pi[k];        if (pattern[i] == pattern[k+1])            k++;        pi[i] = k;    }    return pi;} int kmp(char *target,int tsize,char *pattern,int psize){    int i;    int *pi = compute_prefix_function(pattern,psize);    int k = -1;    if (!pi)        return -1;    for (i = 0; i < tsize; i++) {        while (k > -1 && pattern[k+1] != target[i])            k = pi[k];        if (target[i] == pattern[k+1])            k++;        if (k == psize - 1) {            free(pi);            return i-k;        }    }    free(pi);    return -1;} int main(int argc,const char *argv[]){    char target[] = "ABC ABcdaB ABcdaBcdaBDE";    char *ch = target;    char pattern[] = "ABcdaBD";    int i;     i = kmp(target,strlen(target),pattern,strlen(pattern));    if (i >= 0)        printf("matched @: %s\n",ch + i);    return 0;}/** end of http://code.activestate.com/recipes/577908/ }}} */

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的克努特 - 莫里斯 - 普拉特算法的C语言实现全部内容,希望文章能够帮你解决克努特 - 莫里斯 - 普拉特算法的C语言实现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1231969.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存