C语言字符串匹配函数
C语言字符串匹配函数,保存有需要时可以用:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h> /*
pattern:
pos:
*/ static int badShift[]; static int goodPostfixLastPos(const char *pattern,int pos)
{
#define _break(flag) if(flag){ break;} int flag = ;
int len = strlen(pattern);
int postFix_len = len - pos;
int postFix_position = pos;
int initStart = pos - postFix_len;
int last_start = ;
while(postFix_len)
{
last_start = (postFix_position == pos) ?initStart:;
int postFix_start = postFix_position;
for(;last_start>= && postFix_start<len;last_start++,postFix_start++)
{
flag = (pattern[last_start] == pattern[postFix_start]);
_break(!flag); } _break(flag);
if(initStart >= )
{
initStart--;
}
else
{
postFix_position++;
postFix_len--;
}
} return flag?last_start-:-;
} static int *calc_goodPostfixShift(const char *pattern,int *goodShift)
{
int len = strlen(pattern);
for(int i=;i<len;i++)
{
goodShift[i] = len - goodPostfixLastPos(pattern,i) - ;
} return goodShift;
} static int *clac_badcharShift(const char *ptrn)
{
int i;
int pLen = strlen(ptrn); for(i = ; i < ; i++)
{
*(badShift+i) = pLen;
} while(pLen != )
{
*(badShift+(unsigned char)*ptrn++) = --pLen;
} return badShift;
} int BMSearch(const char *str,const char *pattern)
{ int goodShift[strlen(pattern)];
int len1 = strlen(str);
int len2 = strlen(pattern); clac_badcharShift(pattern);
calc_goodPostfixShift(pattern,goodShift);
for(int i=len2 - ;i<len1;)
{
int start = i;
int pos_pattern = len2 - ;
for(;pos_pattern>=;pos_pattern--,start--)
{
if(str[start] != pattern[pos_pattern])
{
break;
}
}
if(pos_pattern < )
{
return start + ;
} if(pos_pattern == (len2 - ))
{
i += badShift[str[start]];
}
else
{
i += goodShift[pos_pattern + ];
}
} return -;
}
C语言字符串匹配函数的更多相关文章
- 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith
[C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...
- python实现 字符串匹配函数
通配符是 shell 命令中的重要功能,? 表示匹配任意 1 个字符,*表示匹配 0 个或多个字符.请使用你熟悉的编程语言实现一个字符串匹配函数,支持 ? 和 * 通配符.如 "a?cd*d ...
- c语言字符处理函数常见使用集合
1.最近看一些开源项目代码时,总会看到 c 语言中一些 "str" 开头的处理字符串的用法,有的之前没用到过,特此记录,随时看到随时添加. 这里不提出源码,只是一些使用说明加例子 ...
- php -- strstr()字符串匹配函数(备忘)
Learn From: http://blog.csdn.net/morley_wang/article/details/7859922 strstr(string,search) strstr() ...
- 总结C语言字符检测函数:isalnum、isalpha...
前言:最近一直在刷leetcode的题,用到isalnum函数,用man手册查找了一下,总共有13个相关函数如下: #include <ctype.h> int isalnum(int c ...
- C语言-字符操作函数
1字符数组的初始化: 1.1 char string={'c','h','i','n','a'} 1.2char string={"china"}或者去掉{}即char strin ...
- Python中字符串匹配函数startswith()函数
1.函数用途含义 Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在指定范围内 ...
- 函数:MySQL中字符串匹配函数LOCATE和POSITION使用方法
1. 用法一 LOCATE(substr,str) POSITION(substr IN str) 函数返回子串substr在字符串str中第一次出现的位置.如果子串substr在str中不存在,返回 ...
- C语言字符串匹配、goto语句、关机命令使用
1.程序执行修改窗口字体颜色命令: 2.程序执行修改窗口标题命令: 3.程序执行关机倒计时命令: 4.根据提示输入团队名称JYHACK TEAM 根据提示输入团队网址:http://bbs.jyhac ...
随机推荐
- 利用Aspose文档转图片
通过使用Aspose您可以轻松的将您的文档转换成真正的图片格式,最好的保证您的内容将实际可见,与其他格式相比,它并不存在查看工具的安装问题. 准备工作: 1:下载Aspose组件包:http://do ...
- Turn off swi-prolog protocol output of ANSI terminal control sequences
To save a record of program execution in prolog, we use the special predicates: protocol and noproto ...
- android studio 中查找代码中的硬编码
在Android Studio中同时按下Ctrl + Shift+ F 或者其他自定义的快捷键,打开全局搜索,在全局搜索中输入 ^((?!(\*|//)).)+[\u4e00-\u9fa5] 并打勾 ...
- TSPL学习笔记(4):数组相关练习
最近研究函数式编程,都是haskell和scheme交互着看的,所以笔记中两种语言的内容都有,练习一般也都用两种语言分别实现. 本篇练习一些数组有关的问题,之所以与数组相关是因为在命令式编程中以下问题 ...
- 在spring,mybatis整合配置中走的弯路(1)
在接触一个新东西,总免不了走一些弯路,也正是在这些弯路中,我们不断的成长. 从git上把之前写的代码扒下来,看看我在当初使用spring与mybatis中所走的弯路,路过的君子也可引以为戒. < ...
- OllyICE 调试的程序无法处理异常 解决方法
问题描述 在用OllyICE打开可执行文件时出现如下图所示错误 解决方法 1. 选项 -> 调试设置 , 打开调试选项 2. 切换到 异常 页签 3. 取消勾选 忽略(传递给程序)以下异常: 单 ...
- A Year Of Books - 2016 Javaer书单
A Year Of Books - 2016 Javaer书单 (PS:欢迎留言推荐,很多来自白衣大哥的推荐) 1. OS & Networking <编码 : 隐匿在计算机软硬件背后的 ...
- 所有博客已经迁移到个人空间 blog.scjia.cc
所有博客已经迁移到个人空间 blog.scjia.cc
- makeJar
task makeJar(type: Jar) { //指定生成的jar名 baseName 'plugin' //从哪里打包class文件 from('build/intermediates/cla ...
- iOS开发-pod install 出错 The dependency `AFNetworking (~> 2.6.0)` is not used in any concrete target.
低版本的cocoapods的Podfile文件pod install可以正常运行 platform :ios, '8.0' pod 'AFNetworking' 高版本的cocoapods的Podfi ...