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

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

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

C语言字符串匹配函数建议收藏此处内容已经被作者隐藏,请输入验证码查看内容
验证码:
请关注本站微信公众号,回复“”,获取验证码。在微信里搜索“”或者“”或者微信扫描右侧二维码都可以关注本站微信公众号。

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 }

 

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

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