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语言字符串匹配函数的更多相关文章

  1. 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith

    [C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...

  2. python实现 字符串匹配函数

    通配符是 shell 命令中的重要功能,? 表示匹配任意 1 个字符,*表示匹配 0 个或多个字符.请使用你熟悉的编程语言实现一个字符串匹配函数,支持 ? 和 * 通配符.如 "a?cd*d ...

  3. c语言字符处理函数常见使用集合

    1.最近看一些开源项目代码时,总会看到 c 语言中一些  "str" 开头的处理字符串的用法,有的之前没用到过,特此记录,随时看到随时添加. 这里不提出源码,只是一些使用说明加例子 ...

  4. php -- strstr()字符串匹配函数(备忘)

    Learn From: http://blog.csdn.net/morley_wang/article/details/7859922 strstr(string,search) strstr() ...

  5. 总结C语言字符检测函数:isalnum、isalpha...

    前言:最近一直在刷leetcode的题,用到isalnum函数,用man手册查找了一下,总共有13个相关函数如下: #include <ctype.h> int isalnum(int c ...

  6. C语言-字符操作函数

    1字符数组的初始化: 1.1 char string={'c','h','i','n','a'} 1.2char string={"china"}或者去掉{}即char strin ...

  7. Python中字符串匹配函数startswith()函数

    1.函数用途含义 Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在指定范围内 ...

  8. 函数:MySQL中字符串匹配函数LOCATE和POSITION使用方法

    1. 用法一 LOCATE(substr,str) POSITION(substr IN str) 函数返回子串substr在字符串str中第一次出现的位置.如果子串substr在str中不存在,返回 ...

  9. C语言字符串匹配、goto语句、关机命令使用

    1.程序执行修改窗口字体颜色命令: 2.程序执行修改窗口标题命令: 3.程序执行关机倒计时命令: 4.根据提示输入团队名称JYHACK TEAM 根据提示输入团队网址:http://bbs.jyhac ...

随机推荐

  1. C# 匿名委托、匿名方法、匿名对象、Lambda表达式

    一.匿名类型可通过使用 new 运算符和对象初始值创建匿名类型.示例:var v = new { Name = "Micro", Message = "Hello&quo ...

  2. ARCGIS10.1 GeoDatabase深入理解:客户端连接与退出地理数据库时系统表的初始化

    平台软件:ARCIGS10.1 ,SQL Server2008R2 目的:了解客户端在连接arcgis 空间地理数据库后,地理数据库会做些什么样的初始化工作 准备工作: 1.准备好数据库日志文件查看工 ...

  3. LoadRunner性能测试结果分析

    LoadRunner性能测试结果分析http://www.docin.com/p-793607435.html

  4. Oracle导入dmp备份文件到不同的表空间中

    原文链接:http://www.2cto.com/database/201211/171081.html 将DMP导入到不同的表空间中 1,用imp导出数据    cmd进入orcle安装目录bin下 ...

  5. java匿名类

    一般情况下,我们需要声明一个类去继承一个接口,然后再new这个类,赋值给接口.但有时后这个类只会被调用一次,为了调用方便,那么就可以用匿名类来简化这个步骤. interface IKey{ void ...

  6. 自动SPF生成工具

    到openspf网站去自动生成一下,地址是http://old.openspf.org/wizard.html.详细解释见下图关于spf的详细语法请看http://www.openspf.org/SP ...

  7. ux.form.field.KindEditor 所见所得编辑器

    注意需要引入KindEditor相关资源 //所见所得编辑器 Ext.define('ux.form.field.KindEditor', { extend: 'Ext.form.field.Text ...

  8. Oracle数据库入门——高水位线详解

    一.什么是水线(High Water Mark)? 所有的oracle段(segments,在此,为了理解方便,建议把segment作为表的一个同义词) 都有一个在段内容纳数据的上限,我们把这个上限称 ...

  9. nginx后的tomcat获取真实用户ip

    目前大部分获取ip的方式:beat.getRequest().getRemoteAddr()但是,如果通过nginx反向代理的话,就获取不到真实ip,是获取的nginx的ip 需要:添加    pro ...

  10. java框架篇---spring IOC 实现原理

    IOC(DI):其实这个Spring架构核心的概念没有这么复杂,更不像有些书上描述的那样晦涩.java程序员都知道:java程序中的每个业务逻辑至少需要两个或以上的对象来协作完成,通常,每个对象在使用 ...