算法库应用--Brute - Force算法串匹配(顺序串)

学习贺利坚老师关于B-F算法的算法库

数据结构例程——串的模式匹配(Brute-Force算法)_sqstring s, t; strassign(s,"ababcabcacbabcaccab");-CSDN博客

本人规则解析博客

串的匹配 (Brute - Force 算法)_brute force算法-CSDN博客\

版本更新日志

V1.0: 在原有的博客基础上, 加入我构建的算法库, 让博客更完全 , 代码可以运行看效果, 不是让算法仅仅停留在 逻辑方面, 这也是后续我努力的方向, 把这些算法模型, 尽可能的去运用到实际的嵌入式项目中, 把理论变成实物 , 本次也进行了名字优化, 让逻辑更清晰

V1.0

功能函数

利用B-f算法,寻找子串在母串中的位置

/**************************************************
函数名: Find_matching_location
功  能: 利用B-f算法,寻找子串在母串中的位置
参  数: (1)Sequential_string Mother_String:母串(2)Sequential_string Son_String:子串
思  路:  从开始逐个对比,如果对比不成功, 则起始位置前移, 计数器初始化, 直到超出长度或找到
注  意:   我们判断是否找到的标志,就是判断 子串计数器是否对比完,然后跳出
返回值:  int: -1--没找到, 其他:找到,返回其数组位序
**************************************************/
int Find_matching_location(Sequential_string Mother_String,Sequential_string Son_String)
{int Mother_counter,Son_counter;//同步计数器int Mother_begin;//每次对比从母串开始的位置Mother_counter = 0;Son_counter = 0;Mother_counter = 0;//Brute-Force算法while((Mother_begin+Mother_counter) < Mother_String.length && Son_counter < Son_String.length){if(Mother_String.Sequential_string_data[Mother_begin+Mother_counter] == Son_String.Sequential_string_data[Son_counter]){Mother_counter++;Son_counter++;}else{Mother_begin++;Mother_counter = 0;Son_counter = 0;}}//通过判断子串计数器的溢出数值,得出是否找到if(Son_counter >= Son_String.length){return Mother_begin;}else{return (-1);}
}

调用的顺序串算法库

头文件

Sequential_string.h
#ifndef _SEQUENTIAL_STRING_H_INCLUDE
#define _SEQUENTIAL_STRING_H_INCLUDE#include <stdio.h>
#define MaxSize 100 //最多字符个数//顺序串数据结构
typedef struct
{char Sequential_string_data[MaxSize];//数组串数据int length;                          //实际串长
}Sequential_string;//(1)将一个字符串数组赋值给顺序串
void Assignment_Sequential_string(Sequential_string &New_String, char Assign_array[]);
//(2) 复制一个串,到另一个串
void Copy_Sequential_String(Sequential_string &accept_string, Sequential_string copy_string);
//(3)判断两个串是否相等
bool Equal_Sequential_String(Sequential_string judge_string1, Sequential_string judge_string2);
//(4)求顺序串串长
int Length_Sequential_String(Sequential_string measure_string);
//(5)串连接
Sequential_string Connect_Sequential_String(Sequential_string link1, Sequential_string link2);
//(6)求子串(从begin_loation开始的number个字符)
Sequential_string Get_Sequential_Substring(Sequential_string substring, int begin_loation, int number);
//(7)插入串(从从begin_loation开始插入字符串,然后组合成新的串)
Sequential_string Insert_Sequential_String(Sequential_string old_string, int begin_loation,Sequential_string insert_string);
//(8)删除串(从begin 开始的number个字符)
Sequential_string Delete_Sequential_String(Sequential_string old_string, int begin_loation,int number);
//(9)串替换(从begin 开始的number个字符)
Sequential_string Replace_Sequential_String(Sequential_string old_string, int begin_loation,int number,Sequential_string new_string);
//(10)输出展示串
void Display_Sequential_String(Sequential_string show_String);
#endif

库函数

Sequential_string.cpp
#include "Sequential_string.h"/**************************************************
(1)函数名: Assignment_Sequential_string
功  能: 将一个字符串数组赋值给顺序串
参  数: (1)Sequential_string &New_String:创建的新串(2)char Assign_array[]: 原始字符串数组
注  意:  顺序数组,结尾加入'\0'
返回值: 无
**************************************************/
void Assignment_Sequential_string(Sequential_string &New_String, char Assign_array[])
{int counter;for(counter = 0; Assign_array[counter] != '\0'; counter++){New_String.Sequential_string_data[counter] = Assign_array[counter];}New_String.Sequential_string_data[counter] = '\0';New_String.length = counter;    //更新串最大位序
}/**************************************************
(2)函数名: Copy_Sequential_String
功  能: 复制一个串,到另一个串
参  数: (1)Sequential_string &accept_string: 复制成的串(2)Sequential_string copy_string:要复制的串
注  意:  复制成的串,传回的是地址,所以不用传回参数
返回值: 无
**************************************************/
void Copy_Sequential_String(Sequential_string &accept_string, Sequential_string copy_string)
{int counter;for(counter = 0; counter < copy_string.length;counter++){accept_string.Sequential_string_data[counter] = copy_string.Sequential_string_data[counter];}accept_string.Sequential_string_data[counter] = '\0';accept_string.length = copy_string.length;
}
/**************************************************
(3)函数名: Equal_Sequential_String
功  能: 判断两个串是否相等
参  数: (1)Sequential_string judge_string1:第一个串(2)Sequential_string judge_string2:第二个串
返回值: bool?是否相等,true:false
**************************************************/
bool Equal_Sequential_String(Sequential_string judge_string1, Sequential_string judge_string2)
{bool same = true;int counter;if(judge_string1.length != judge_string2.length){same = false;}else{for(counter = 0; counter < judge_string1.length;counter++){if(judge_string1.Sequential_string_data[counter] != judge_string2.Sequential_string_data[counter]){same = false;break;}}}return same;}/**************************************************
(4)函数名: Length_Sequential_String
功  能: 求顺序串串长
参  数: Sequential_string measure_string:要进行测量的串
返回值: int:顺序串长度信息
**************************************************/
int Length_Sequential_String(Sequential_string measure_string)
{return measure_string.length;
}/**************************************************
(5)函数名: Connect_Sequential_String
功  能: 把两个串连接成一个串
参  数: Sequential_string link1, Sequential_string link2:两个要链接的串
返回值: 返回Sequential_string Connection_string: 链接成的串
**************************************************/
Sequential_string Connect_Sequential_String(Sequential_string link1, Sequential_string link2)
{Sequential_string Connection_string;int counter;Connection_string.length = link1.length + link2.length;//将第一个串加入链接的串for(counter = 0; counter < link1.length; counter++){Connection_string.Sequential_string_data[counter] = link1.Sequential_string_data[counter];}//将第二个串加入链接的串for(counter = 0; counter < link2.length; counter++){Connection_string.Sequential_string_data[link1.length+counter] = link2.Sequential_string_data[counter];}Connection_string.Sequential_string_data[link1.length+counter] = '\0';return Connection_string;
}/**************************************************
(6)函数名: Get_Sequential_Substring
功  能: 求子串(从begin_loation开始的number个字符)
参  数: (1)Sequential_string mother_String:母串(2)int begin_loation:开始分割子串的位置(3)int number:子串的数量
返回值: Sequential_string son_String:得到的子串
**************************************************/
Sequential_string Get_Sequential_Substring(Sequential_string mother_String, int begin_loation, int number)
{Sequential_string son_String;int counter;son_String.length = 0;if(begin_loation <= 0 || begin_loation > mother_String.length || number < 0 || begin_loation+number-1>mother_String.length){//错误:分割的子字符串的位置错误。printf("\nError<6>:The position of the divided substring is wrong.\n");return son_String; //    参数不正确返回空串}for(counter = begin_loation-1; counter < begin_loation+number-1; counter++){son_String.Sequential_string_data[counter-begin_loation+1] = mother_String.Sequential_string_data[counter];}son_String.Sequential_string_data[counter-begin_loation+1] = '\0';son_String.length = number;return son_String;
}/**************************************************
(7)函数名: Insert_Sequential_String
功  能: 插入串(从从begin_loation开始插入字符串,然后组合成新的串)
参  数: (1)Sequential_string old_string:在原始串的基础上插入(2)int begin_loation: 插入的位置(3)Sequential_string insert_string:插入的新串
思  路:  在原有串的基础上,割开一个口子,放上新串,然后组合成新串
返回值: Sequential_string form_string:组合成的新串
**************************************************/
Sequential_string Insert_Sequential_String(Sequential_string old_string, int begin_loation,Sequential_string insert_string)
{int counter;Sequential_string form_string;form_string.length = 0;//参数不正确, 返回空串if(begin_loation <= 0 || begin_loation > old_string.length+1){//错误:插入位置错误printf("\nError<7>: wrong insertion position.\n");return form_string;}for(counter = 0; counter < begin_loation-1;counter++){form_string.Sequential_string_data[counter] = old_string.Sequential_string_data[counter];}for(counter = 0; counter < insert_string.length;counter++){form_string.Sequential_string_data[begin_loation-1+counter] = insert_string.Sequential_string_data[counter];}for(counter = begin_loation-1; counter<old_string.length;counter++){form_string.Sequential_string_data[insert_string.length+counter] = old_string.Sequential_string_data[counter];}form_string.Sequential_string_data[insert_string.length+counter] = '\0';form_string.length = old_string.length + insert_string.length;return form_string;}
/**************************************************
(8)函数名: Delete_Sequential_String
功  能: 删除串(从begin 开始的number个字符)
参  数: (1)Sequential_string old_string:在原有串的基础上删除(2)int begin_loation: 开始删除的位置(从逻辑1开始)(3)int number:删除的数量
注  意:  要判断删除的位置和数量是否正确
返回值:Sequential_string new_string:删除完后的新串
**************************************************/
Sequential_string Delete_Sequential_String(Sequential_string old_string, int begin_loation,int number)
{int counter;//定义计数器Sequential_string new_string;new_string.length = 0;//合法性判断(begin_loation理应从1开始到leng长度)if(begin_loation <= 0 || begin_loation > old_string.length || (begin_loation+number-1) > old_string.length){//错误:删除的位置或数量错误。printf("Error<8>: Wrong location or quantity of deletion.");return new_string;//返回空串}//择出删除位置之前的串for(counter = 0; counter < begin_loation-1;counter++){new_string.Sequential_string_data[counter] = old_string.Sequential_string_data[counter];}//择出删除位置之后的串for(counter = begin_loation+number-1; counter < old_string.length; counter++){new_string.Sequential_string_data[counter-number] = old_string.Sequential_string_data[counter];}new_string.Sequential_string_data[counter-number] = '\0';new_string.length = old_string.length - number;return new_string;
}/**************************************************
(9)函数名: Replace_Sequential_String
功  能: 串替换(从begin 开始的number个字符)
参  数: (1)Sequential_string old_string:原始串(2)int begin_loation:开始替换的位置(3)int number:替换的字符个数(4)Sequential_string replace_string:要替换成的字符串
思  路: 锁定old_string从begin_loation开始的number个字符,然后开始剪切建立新串,①把begin_loation之前的字符加入新串,②要替换成的串加入,③锁定后的字符加入④组合成新串,返回传出
注  意:  最后加'\0'
返回值: Sequential_string new_string:替换后,产生的新串
**************************************************/
Sequential_string Replace_Sequential_String(Sequential_string old_string, int begin_loation,int number,Sequential_string replace_string)
{int counter;Sequential_string new_string;new_string.length = 0;//合法性判断if(begin_loation <= 0 || begin_loation > old_string.length || begin_loation+number-1>old_string.length){//错误:要替换位置出现错误printf("\nError<9>: There is an error in the position to be replaced.\n");return new_string;//返回空串}//开始复制剪切for(counter = 0; counter < begin_loation-1; counter++){new_string.Sequential_string_data[counter] = old_string.Sequential_string_data[counter];}//加入要替换的串for(counter = 0; counter < replace_string.length; counter++){new_string.Sequential_string_data[begin_loation-1+counter] = replace_string.Sequential_string_data[counter];}//被替换位置,后面剩余的串for(counter = begin_loation+number-1; counter < old_string.length; counter++){new_string.Sequential_string_data[counter-number+replace_string.length] = old_string.Sequential_string_data[counter];}new_string.Sequential_string_data[counter-number+replace_string.length] = '\0';new_string.length = old_string.length - number + replace_string.length;return new_string;
}/**************************************************
(10)函数名: Display_Sequential_String
功  能: 输出展示串
参  数: Sequential_string show_String:要输出展示的串
注  意: 字符串后续可以换成自定义类型
返回值: 无
**************************************************/
void Display_Sequential_String(Sequential_string show_String)
{int counter;if(show_String.length > 0){for(counter = 0; counter < show_String.length; counter++){printf("%c", show_String.Sequential_string_data[counter]);}printf("\n");}
}

main函数调用

int main()
{//在母串里面找子串Sequential_string Mother_String;Sequential_string Son_String;char Mother_String_array[] = {'a','b','a','b','c','a','b','c','a','c','b','a','b','\0'};char Son_String_array[] = {'a','b','c','a','c','\0'};//把两个字符串创建成字符串Assignment_Sequential_string(Mother_String, Mother_String_array);Assignment_Sequential_string(Son_String, Son_String_array);printf("\nMother_String:\n");Display_Sequential_String(Mother_String);printf("\nSon_String:\n");Display_Sequential_String(Son_String);printf("\n%d\n",Find_matching_location(Mother_String,Son_String));return 0;
}

main.cpp源码(包含功能函数)

main.cpp
#include <stdio.h>
#include "Sequential_string.h"/**************************************************
函数名: Find_matching_location
功  能: 利用B-f算法,寻找子串在母串中的位置
参  数: (1)Sequential_string Mother_String:母串(2)Sequential_string Son_String:子串
思  路:  从开始逐个对比,如果对比不成功, 则起始位置前移, 计数器初始化, 直到超出长度或找到
注  意:   我们判断是否找到的标志,就是判断 子串计数器是否对比完,然后跳出
返回值:  int: -1--没找到, 其他:找到,返回其数组位序
**************************************************/
int Find_matching_location(Sequential_string Mother_String,Sequential_string Son_String)
{int Mother_counter,Son_counter;//同步计数器int Mother_begin;//每次对比从母串开始的位置Mother_counter = 0;Son_counter = 0;Mother_counter = 0;//Brute-Force算法while((Mother_begin+Mother_counter) < Mother_String.length && Son_counter < Son_String.length){if(Mother_String.Sequential_string_data[Mother_begin+Mother_counter] == Son_String.Sequential_string_data[Son_counter]){Mother_counter++;Son_counter++;}else{Mother_begin++;Mother_counter = 0;Son_counter = 0;}}//通过判断子串计数器的溢出数值,得出是否找到if(Son_counter >= Son_String.length){return Mother_begin;}else{return (-1);}
}int main()
{//在母串里面找子串Sequential_string Mother_String;Sequential_string Son_String;char Mother_String_array[] = {'a','b','a','b','c','a','b','c','a','c','b','a','b','\0'};char Son_String_array[] = {'a','b','c','a','c','\0'};//把两个字符串创建成字符串Assignment_Sequential_string(Mother_String, Mother_String_array);Assignment_Sequential_string(Son_String, Son_String_array);printf("\nMother_String:\n");Display_Sequential_String(Mother_String);printf("\nSon_String:\n");Display_Sequential_String(Son_String);printf("\n%d\n",Find_matching_location(Mother_String,Son_String));return 0;
}

运行结果展示:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1473602.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

在5G/6G应用中实现高性能放大器的建模挑战

来源&#xff1a;Modelling Challenges for Enabling High Performance Amplifiers in 5G/6G Applications {第28届“集成电路和系统的混合设计”(Mixed Design of Integrated Circuits and Systems)国际会议论文集&#xff0c;2021年6月24日至26日&#xff0c;波兰洛迪} 本文讨…

跟着峰哥学java 第四天 商品分类 前后端显示

1.后端 1.1mybatis-plus分页查询配置 在商品热卖数据中&#xff0c;只让其显示八条数据 将要使用分页 也就是service.page方法 此时需要配置 mp拦截器 Configuration public class MybatisPlusConfig {Beanpublic PaginationInterceptor paginationInterceptor() {return …

宝可梦 第一到第五时代 神兽 幻兽 准神宝可梦盘点

小时候特别喜欢看宝可梦 也玩过一些宝可梦类游戏 而宝可梦中 大家最喜欢的莫过于神兽 今天 我们来盘点一下 宝可梦各世代的神兽 以及准神宝可梦 第一世代 一级神 超梦 属性: 超能力 是火箭队根据梦幻基因制造的一只人造传说宝可梦。 一直是一只热度非常高的宝可梦&#xf…

图书管理系统 全栈项目分享

文章目录 项目简要说明项目开源地址b站视频演示技术栈部分效果展示 项目简要说明 本项目是我的数据库课设&#xff0c;个人感觉做得还行&#xff0c;目前项目开源&#xff0c;README文档里有项目的介绍和使用说明&#xff0c;这里就不一一赘述了 项目开源地址 github - libr…

MobaXterm不显示隐藏文件

MobaXterm在左边显示隐藏文件&#xff0c;以.开头的文件&#xff0c;想让它不显示&#xff0c;点击红框按钮就可以了

Ubuntu 20版本安装Redis教程

第一步 切换到root用户&#xff0c;使用su命令&#xff0c;进行切换。 输入&#xff1a; su - 第二步 使用apt命令来搜索redis的软件包&#xff0c;输入命令&#xff1a;apt search redis 第三步 选择需要的redis版本进行安装&#xff0c;本次选择默认版本&#xff0c;redis5.…

嵌入式C语言面试相关知识——关键字(不定期更新)

嵌入式C语言面试相关知识——关键字 一、博客声明二、C语言关键字1、sizeof关键字2、static关键字3、const关键字4、volatile关键字5、extern关键字 一、博客声明 又是一年一度的秋招&#xff0c;怎么能只刷笔试题目呢&#xff0c;面试题目也得看&#xff0c;想当好厂的牛马其实…

golang结合neo4j实现权限功能设计

neo4j 是非关系型数据库之图形数据库&#xff0c;这里不再赘述。 传统关系数据库基于rbac实现权限, user ---- role ------permission,加上中间表共5张表。 如果再添上部门的概念&#xff1a;用户属于部门&#xff0c;部门拥有 角色&#xff0c;则又多了一层&#xff1a; user-…

小暑节气,选对劳保鞋,让安全与清凉同行

在七月炽热的阳光下&#xff0c;我们迎来了二十四节气中的小暑&#xff0c;标志着盛夏时节的正式开始。随着气温的节节攀升&#xff0c;不仅大自然万物进入了生长的旺季&#xff0c;我们的工作与日常生活也面临着新的挑战——如何在高温环境下保障自身安全&#xff0c;成为了不…

计算机网络——数据链路层(以太网)

目录 局域网的数据链路层 局域网可按照网络拓扑分类 局域网与共享信道 以太网的两个主要标准 适配器与mac地址 适配器的组成与运作 MAC地址 MAC地址的详细介绍 局域网的mac地址格式 mac地址的发送顺序 单播、多播&#xff0c;广播mac地址 mac帧 如何取用…

Spring源码十四:Spring生命周期

上一篇我们在Spring源码十三&#xff1a;非懒加载单例Bean中看到了Spring会在refresh方法中去调用我们的finishBeanFactoryInitialization方法去实例化&#xff0c;所有非懒加载器单例的bean。并实例化后的实例放到单例缓存中。到此我们refresh方法已经接近尾声。 Spring的生命…

提升系统稳定性:熔断、降级和限流策略详解

文章目录 前言一、熔断&#xff08;Circuit Breaker&#xff09;二、降级&#xff08;Degradation&#xff09;三、限流&#xff08;Rate Limiting&#xff09;四、应用案例五、小结推荐阅读 前言 随着互联网业务的快速发展&#xff0c;系统稳定性和高可用性成为现代分布式系统…

【Python机器学习】算法链与管道——网格搜索预处理步骤与模型参数

我们可以利用管道将机器学习工作流程中的所有处理步骤封装成一个scikit-learn估计器。这么做的好处在于&#xff1a;现在我们可以使用监督任务&#xff08;分类或回归&#xff09;的输出来调节预处理参数。 下面用一个管道来完成一个建模过程。管道包含了3个步骤&#xff1a;缩…

ELK优化之Filebeat部署

目录 1.安装配置Nginx 2.安装 Filebeat 3.设置 filebeat 的主配置文件 4.修改Logstash配置 5.启动配置 6.kibana验证 主机名ip地址主要软件es01192.168.9.114ElasticSearches02192.168.9.115ElasticSearches03192.168.9.116ElasticSearch、Kibananginx01192.168.9.113ng…

【C语言】register 关键字

在C语言中&#xff0c;register关键字用于提示编译器将变量尽量存储在CPU的寄存器中&#xff0c;而不是在内存中。这是为了提高访问速度&#xff0c;因为寄存器的访问速度比内存快得多。使用register关键字的变量通常是频繁使用的局部变量。 基本用法 void example() {regist…

iOS多target时怎么对InfoPlist进行国际化

由于不同target要显示不同的App名称、不同的权限提示语&#xff0c;国际化InfoPlist文件必须创建名称为InfoPlist.strings的文件&#xff0c;那么多个target时怎么进行国际化呢&#xff1f;步骤如下&#xff1a; 一、首先我们在项目根目录创建不同的文件夹对应多个不同的targe…

自闭症儿童的治疗方法有哪些?

身为星贝育园自闭症儿童康复学校的资深教育者&#xff0c;我深知自闭症谱系障碍&#xff08;ASD&#xff09;儿童的教育与治疗需要一个全面、个性化的方案。在星贝育园&#xff0c;我们致力于为孩子们提供一个充满爱与理解的环境&#xff0c;采用多种科学验证的教育方法&#x…

商贸物流大脑:大模型+数据要素赋能智慧物流数据平台

项目背景与意义 物流行业快速发展&#xff0c;数据量急剧增加&#xff0c;随着电子商务、智能制造等领域的快速发展&#xff0c;物流行业面领着前所未有的挑战和机遇&#xff0c;如效率低下、资源配置不均、信息不透明等问题。随着全球化和电子商务的快速发展&#xff0c;数据…

Arthas实战(5)- 项目性能调优

1、接口耗时查询&#xff1a;trace命令 trace 命令能主动搜索 class-pattern&#xff0f;method-pattern 对应的方法调用路径&#xff0c;渲染和统计整个调用链路上的所有性能开销和追踪调用链路。 1.1 准备测试应用 新建一个 SpringBoot 应用&#xff0c;写一耗时久的代码&…

编写优雅Python代码的20个最佳实践

想要让你的代码像艺术品一样既实用又赏心悦目吗&#xff1f;今天我们就来聊聊如何通过20个小技巧&#xff0c;让你的Python代码从平凡走向优雅&#xff0c;让同行看了都忍不住点赞&#xff01; **温馨提示&#xff1a;更多的编程资料&#xff0c;领取方式在&#xff1a; 1. 拥…