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 ...
随机推荐
- 使用Ant编译提示Class not found: javac1.8
无论是使用Eclipse还是使用Ant命令,都可能会在编译时遇到提示:Class not found: javac1.8 今天用Ant打包Android,apk,运行出现了batch_build.xm ...
- Asp.net Core中使用NLog,并封装成公共的日志方法
1.安装NLog "NLog.Extensions.Logging": "1.0.0-rtm-alpha4" 2.配置NLog public void Conf ...
- Unity3D常用代码总结
1 GUI汇总 function OnGUI() { GUI.Label(Rect(1,1,100,20),"I'm a Label"); //1 GUI.Box(Rect(1,2 ...
- windbg常用命令
SRV*C:\Symbols*http://msdl.microsoft.com/download/symbols CPU常用命令 载入sos.dll 执行.load C:\Windows\Micr ...
- 8个经典炫酷的HTML5 Canvas动画欣赏
HTML5非常强大,尤其是Canvas技术的应用,让HTML5几乎可以完成所有Flash能完成的效果.本文精选了8个经典炫酷的HTML5 Canvas动画欣赏,每一个都提供全部的源代码,希望对你有所帮 ...
- swift 枚举类型
1:swift的枚举类型是一系列的值,不同于c语言中枚举类型是整数类型.每个枚举定义了个新的类型 2:switch类型匹配 2.1枚举类型和switch单个匹配 enum PlatType{ case ...
- EPLAN P8导线颜色的设置
P8里的导线应称为"连接",连接的颜色代表了其电位的传递路径,如可以给三相电源设置成黑色,PE设为绿色,N设为蓝色等等. P8中电位在连接和元器件中传递的方法是由元器件的连接点属 ...
- ios 关于文件操作 获取 文件大小
分类: Apple IPhone2012-06-28 11:31 4664人阅读 评论(0) 收藏 举报 ios语言manager测试c c语言 实现 #include "sys/stat ...
- iOS 7新功能例子
参考https://github.com/shu223/iOS7-Sampler Code examples for the new functions of iOS 7. Contents Dyna ...
- mybatis配置文件(其中,注意节点先后顺序)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC & ...