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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux 安装tinyxml,使用TinyXml「建议收藏」

    linux 安装tinyxml,使用TinyXml「建议收藏」使用TinyXml的两种方法。第一,导入所需的头文件和cpp文件TinyXml由两个头文件(.h文件)和四个CPP文件(.cpp文件)构成,用的时候,只要将(tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp)导入工程就可以用它的东西了。这就是开源的好处,就跟你自己写的程序一样,想怎么用都行。…

    2022年6月11日
  • SQLldr_乔羽简介

    SQLldr_乔羽简介1.SQLLDR导入 1.1 简介 SQL*LOADER是ORACLE的数据加载工具,通常用来将操作系统文件(数据)迁移到ORACLE数据库中。SQL*LOADER是大型数据仓库选择使用的加载方法,因为它提供了最快速的途径(DIRECT,PARALLEL)。 2.2 语法和参数语法:SQLLDRkeyword=value[,keyword=value,…];…

    2022年4月19日
  • Apache 模块 mod rewrite RewriteCond 通过检查HTTP REFERER避免

    Apache 模块 mod rewrite RewriteCond 通过检查HTTP REFERER避免Apache模块modrewriteRewriteCond通过检查HTTPREFERER避免

    2022年5月13日
  • Python网页爬取_在pycharm里面如何爬取网页

    Python网页爬取_在pycharm里面如何爬取网页使用Python爬取简单数据闲暇时间学习Python,不管以后能否使用,就算了解计算机语言知识。因为有一点Java基础,所以Python的基本语法就很快的过了一遍,看了视频之后直接就开始编写,表达或许有点混乱,以后慢慢改进。一、导入爬取网页所需的包。frombs4importBeautifulSoup#网页解析importxlwt#excelimportre#正则表达式importurllib.request,urllib.error#指定url,获取

    2022年10月13日
  • getopt解析

    getopt解析转载自http://www.cnitblog.com/zouzheng/archive/2007/04/02/25034.aspxgetopt被用来解析命令行选项参数。就不用自己写东东处理argv了。#include      externchar*optarg;  //选项的参数指针      externintoptind,  //下一次调用getopt的时,从

    2022年5月2日
  • 计算机-普林斯顿结构

    计算机-普林斯顿结构冯·诺伊曼结构,也称冯·诺伊曼模型或普林斯顿结构,是一种将程序指令存储器和数据存储器合并在一起的计算机设计概念结构。依据冯·诺伊曼结构设计出的计算机称做冯.诺依曼计算机,又称存储程序计算机。特点结构…

    2022年10月5日

发表回复

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

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