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 ...
随机推荐
- Android Dagger依赖注入框架浅析
今天接触了Dagger这套android的依赖注入框架(DI框架).感觉跟Spring 的IOC差点儿相同吧.这个框架它的优点是它没有採用反射技术(Spring是用反射的),而是用预编译技术.因为基于 ...
- 阿里云里面的Linux 系统挂载数据盘
转自:http://www.cnblogs.com/adjk/p/5112360.html 适用系统:非IO优化+SSD云盘Linux(Redhat , CentOS,Debian,Ubuntu)实例 ...
- Oracle存储过程,以逗号分隔字符串传参的处理
Oracle存储过程,经常会遇见传入的参数是逗号分隔. 处理需要3步: 第一步,创建Type类型 第二部,创建函数 第三部,创建存储过程 代码如下: 第一步: create or replace ty ...
- struts2:非表单标签
非表单标签主要用于输出在Action中封装的信息,这在实际运用中是很常见的. 1. actionerror标签 <s:actionerror>标签主要用于输出错误信息到客户端,该标签将Ac ...
- 【NLP】word2vec
http://blog.csdn.net/mytestmy/article/details/26969149?utm_source=tuicool&utm_medium=referral
- 【python】日志模块
# -*- coding: utf-8 -*- """ Created on Thu Jul 09 09:36:59 2015 @author: dapenghuang ...
- Spring3系列9- Spring AOP——Advice
Spring3系列9- Spring AOP——Advice Spring AOP即Aspect-oriented programming,面向切面编程,是作为面向对象编程的一种补充,专门用于处理系统 ...
- IIS7.5配置SSL
1,首先需要准备两个证书(CA证书,服务器证书). CA证书由公共的CA机构提供,widnow系统内部已经内置了很多这类证书,如图(日文系统). 服务器证书是导入到IIS里面用的. 2,有了上面的认识 ...
- MySQL降权:MySQL以Guests帐户启动设置方法
MySQL安装到Windows上,默认是以SYSTEM权限运行,如下图: SYSTEM是超级管理员.不是必须,不推荐用此权限运行任何程序. 本文将演示如何在GUEST帐户下运行MySQL. 第一步:建 ...
- 64bit ubuntu 安装32bit的软件
在64bit的系统上安装32bit的软件时,会提示: file not found or no such file. 此时只需要安装 sudo apt-get install libc6-dev-i3 ...