C语言字符串匹配函数建议收藏

C语言字符串匹配函数,保存有需要时可以用:1#include2#include3#include4#include5#include67/*8pattern:9pos

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

全栈程序员社区此处内容已经被作者隐藏,请输入验证码查看内容
验证码:
请关注本站微信公众号,回复“验证码”,获取验证码。在微信里搜索“全栈程序员社区”或者“www_javaforall_cn”或者微信扫描右侧二维码都可以关注本站微信公众号。

C语言字符串匹配函数,保存有需要时可以用:

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <assert.h>
  5 #include <time.h>
  6 
  7 /*
  8 pattern:
  9 pos:
 10 */
 11 
 12 static int badShift[256];
 13 
 14 
 15 static int goodPostfixLastPos(const char *pattern,int pos)
 16 {
 17 #define _break(flag) if(flag){ break;}
 18 
 19     int flag = 0;
 20     int len = strlen(pattern);
 21     int postFix_len = len - pos;
 22     int postFix_position = pos;
 23     int initStart = pos - postFix_len;
 24     int last_start = 0;
 25     while(postFix_len)
 26     {
 27         last_start = (postFix_position == pos) ?initStart:0;
 28         int postFix_start = postFix_position;
 29         for(;last_start>=0 && postFix_start<len;last_start++,postFix_start++)
 30         {
 31             flag = (pattern[last_start] == pattern[postFix_start]);
 32             _break(!flag);
 33 
 34         }
 35 
 36         _break(flag);
 37         if(initStart >= 0)
 38         {
 39             initStart--;
 40         }
 41         else
 42         {
 43             postFix_position++;
 44             postFix_len--;
 45         }
 46     }
 47 
 48     return flag?last_start-1:-1;
 49 }
 50 
 51 static int *calc_goodPostfixShift(const char *pattern,int *goodShift)
 52 {
 53     int len = strlen(pattern);
 54     for(int i=0;i<len;i++)
 55     {
 56         goodShift[i] = len - goodPostfixLastPos(pattern,i) - 1;
 57     }
 58 
 59     return goodShift;
 60 }
 61 
 62 static int *clac_badcharShift(const char *ptrn)
 63 {
 64     int i;
 65     int pLen = strlen(ptrn);
 66 
 67     for(i = 0; i < 256; i++)
 68     {
 69         *(badShift+i) = pLen;
 70     }
 71 
 72     while(pLen != 0)
 73     {
 74         *(badShift+(unsigned char)*ptrn++) = --pLen;
 75     }
 76 
 77     return badShift;
 78 }
 79 
 80 int BMSearch(const char *str,const char *pattern)
 81 {
 82     
 83     int goodShift[strlen(pattern)];
 84     int len1 = strlen(str);
 85     int len2 = strlen(pattern);
 86 
 87     clac_badcharShift(pattern);
 88     calc_goodPostfixShift(pattern,goodShift);
 89     for(int i=len2 - 1;i<len1;)
 90     {
 91         int start = i;
 92         int pos_pattern = len2 - 1;
 93         for(;pos_pattern>=0;pos_pattern--,start--)
 94         {
 95             if(str[start] != pattern[pos_pattern])
 96             {
 97                 break;
 98             }
 99         }
100         if(pos_pattern < 0)
101         {
102             return start + 1;
103         }
104 
105         if(pos_pattern == (len2 - 1))
106         {
107             i += badShift[str[start]];
108         }
109         else
110         {
111             i += goodShift[pos_pattern + 1];
112         }
113     }
114 
115     return -1;
116 }

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/120792.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Python之matplotlib库建议收藏

    知识结构pyplot.plot()流程1._axes.py中plot()函数说明a.调用说明plot([x],y,[fmt],data=None,**kwargs)plot([x

    2021年12月19日
    46
  • Ubuntu下插入网线无法联网的问题

    Ubuntu下插入网线无法联网的问题今天把以前的服务器搬出来,准备训练一个深度学习模型,然而,在联网的过程中,出现一个问题:就是插入网线后无法联网。想到以前配置过翻墙,就把相关的配置文件如.bashrc,/etc/profile,等相关文件进行了修改,屏蔽掉以前的翻墙代理设置,然而还是无法联网。后面想到以前是用拨号INodeClient来连接上网的,就把与InodeClient相关的配置注释掉,然而还是无法上网。后面在网上找到一个解决方案:参考网址https://blog.csdn.net/zhu334974857/articl.

    2022年6月26日
    94
  • JavaScript 学习笔记——cssText

    JavaScript 学习笔记——cssText平常编写代码,更改一个元素样式的时候,自己都是用obj.style.width=”200px”;obj.style.position=”absolute”;obj.style.left=”100px”;之类的代码进行设置,这样的话如果更改样式很多的时候,就要写很多代码,难道不能像Jquery那样使用$(obj).css(……);这样进行设置么?于是自己搜了下使用Javascript批

    2022年7月14日
    17
  • java debug调试怎么用?[通俗易懂]

    java debug调试怎么用?[通俗易懂]我的qq2038373094在做项目的时候,尤其是涉及多个页面的传值的时候,debug调试十分有用,可以迅速帮你找到错误的原因!用debug的好处:1.跟踪变量,可以查看变量的值的变化2.迅速找到错误的原因,节省时间,找错小帮手那么debug调试助手怎么用,用在什么地方?debug用在java程序上,.class文件上面不适合jsp页面,不适合镶嵌在jsp页面…

    2022年10月16日
    2
  • WinExec, ShellExecute, CreateProcess

    WinExec, ShellExecute, CreateProcess2010-08-1314:47 633人阅读 评论(0) 收藏 举报vc++nullattributes文档microsoftsecurity在vc++程序中运行另一个程序的方法有三个:WinExec(),ShellExcute()和CreateProcess()三个SDK函数: WinExec,ShellExecute ,CreateProcess可以实现调

    2022年7月27日
    3
  • gridlayout布局

    gridlayout布局浅谈android4.0开发之GridLayout布局分类: Android应用开发技巧2012-03-1123:51 3646人阅读 评论(1) 收藏 举报androidlayoutbuttonencoding框架编程作者:李响         本文重点讲述了自android4.0版本后新增的GridLayout网格布局的一些基本

    2022年6月1日
    58

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号