当前位置:   article > 正文

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

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

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

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

本人规则解析博客

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

版本更新日志

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

V1.0

功能函数

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

  1. /**************************************************
  2. 函数名: Find_matching_location
  3. 功 能: 利用B-f算法,寻找子串在母串中的位置
  4. 参 数: (1)Sequential_string Mother_String:母串
  5. (2)Sequential_string Son_String:子串
  6. 思 路: 从开始逐个对比,如果对比不成功, 则起始位置前移, 计数器初始化, 直到超出长度或找到
  7. 注 意: 我们判断是否找到的标志,就是判断 子串计数器是否对比完,然后跳出
  8. 返回值: int: -1--没找到, 其他:找到,返回其数组位序
  9. **************************************************/
  10. int Find_matching_location(Sequential_string Mother_String,Sequential_string Son_String)
  11. {
  12. int Mother_counter,Son_counter;//同步计数器
  13. int Mother_begin;//每次对比从母串开始的位置
  14. Mother_counter = 0;
  15. Son_counter = 0;
  16. Mother_counter = 0;
  17. //Brute-Force算法
  18. while((Mother_begin+Mother_counter) < Mother_String.length && Son_counter < Son_String.length)
  19. {
  20. if(Mother_String.Sequential_string_data[Mother_begin+Mother_counter] == Son_String.Sequential_string_data[Son_counter])
  21. {
  22. Mother_counter++;
  23. Son_counter++;
  24. }
  25. else
  26. {
  27. Mother_begin++;
  28. Mother_counter = 0;
  29. Son_counter = 0;
  30. }
  31. }
  32. //通过判断子串计数器的溢出数值,得出是否找到
  33. if(Son_counter >= Son_String.length)
  34. {
  35. return Mother_begin;
  36. }
  37. else
  38. {
  39. return (-1);
  40. }
  41. }

调用的顺序串算法库

头文件

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

库函数

Sequential_string.cpp
  1. #include "Sequential_string.h"
  2. /**************************************************
  3. (1)函数名: Assignment_Sequential_string
  4. 功 能: 将一个字符串数组赋值给顺序串
  5. 参 数: (1)Sequential_string &New_String:创建的新串
  6. (2)char Assign_array[]: 原始字符串数组
  7. 注 意: 顺序数组,结尾加入'\0'
  8. 返回值: 无
  9. **************************************************/
  10. void Assignment_Sequential_string(Sequential_string &New_String, char Assign_array[])
  11. {
  12. int counter;
  13. for(counter = 0; Assign_array[counter] != '\0'; counter++)
  14. {
  15. New_String.Sequential_string_data[counter] = Assign_array[counter];
  16. }
  17. New_String.Sequential_string_data[counter] = '\0';
  18. New_String.length = counter; //更新串最大位序
  19. }
  20. /**************************************************
  21. (2)函数名: Copy_Sequential_String
  22. 功 能: 复制一个串,到另一个串
  23. 参 数: (1)Sequential_string &accept_string: 复制成的串
  24. (2)Sequential_string copy_string:要复制的串
  25. 注 意: 复制成的串,传回的是地址,所以不用传回参数
  26. 返回值: 无
  27. **************************************************/
  28. void Copy_Sequential_String(Sequential_string &accept_string, Sequential_string copy_string)
  29. {
  30. int counter;
  31. for(counter = 0; counter < copy_string.length;counter++)
  32. {
  33. accept_string.Sequential_string_data[counter] = copy_string.Sequential_string_data[counter];
  34. }
  35. accept_string.Sequential_string_data[counter] = '\0';
  36. accept_string.length = copy_string.length;
  37. }
  38. /**************************************************
  39. (3)函数名: Equal_Sequential_String
  40. 功 能: 判断两个串是否相等
  41. 参 数: (1)Sequential_string judge_string1:第一个串
  42. (2)Sequential_string judge_string2:第二个串
  43. 返回值: bool?是否相等,true:false
  44. **************************************************/
  45. bool Equal_Sequential_String(Sequential_string judge_string1, Sequential_string judge_string2)
  46. {
  47. bool same = true;
  48. int counter;
  49. if(judge_string1.length != judge_string2.length)
  50. {
  51. same = false;
  52. }
  53. else
  54. {
  55. for(counter = 0; counter < judge_string1.length;counter++)
  56. {
  57. if(judge_string1.Sequential_string_data[counter] != judge_string2.Sequential_string_data[counter])
  58. {
  59. same = false;
  60. break;
  61. }
  62. }
  63. }
  64. return same;
  65. }
  66. /**************************************************
  67. (4)函数名: Length_Sequential_String
  68. 功 能: 求顺序串串长
  69. 参 数: Sequential_string measure_string:要进行测量的串
  70. 返回值: int:顺序串长度信息
  71. **************************************************/
  72. int Length_Sequential_String(Sequential_string measure_string)
  73. {
  74. return measure_string.length;
  75. }
  76. /**************************************************
  77. (5)函数名: Connect_Sequential_String
  78. 功 能: 把两个串连接成一个串
  79. 参 数: Sequential_string link1, Sequential_string link2:两个要链接的串
  80. 返回值: 返回Sequential_string Connection_string: 链接成的串
  81. **************************************************/
  82. Sequential_string Connect_Sequential_String(Sequential_string link1, Sequential_string link2)
  83. {
  84. Sequential_string Connection_string;
  85. int counter;
  86. Connection_string.length = link1.length + link2.length;
  87. //将第一个串加入链接的串
  88. for(counter = 0; counter < link1.length; counter++)
  89. {
  90. Connection_string.Sequential_string_data[counter] = link1.Sequential_string_data[counter];
  91. }
  92. //将第二个串加入链接的串
  93. for(counter = 0; counter < link2.length; counter++)
  94. {
  95. Connection_string.Sequential_string_data[link1.length+counter] = link2.Sequential_string_data[counter];
  96. }
  97. Connection_string.Sequential_string_data[link1.length+counter] = '\0';
  98. return Connection_string;
  99. }
  100. /**************************************************
  101. (6)函数名: Get_Sequential_Substring
  102. 功 能: 求子串(从begin_loation开始的number个字符)
  103. 参 数: (1)Sequential_string mother_String:母串
  104. (2)int begin_loation:开始分割子串的位置
  105. (3)int number:子串的数量
  106. 返回值: Sequential_string son_String:得到的子串
  107. **************************************************/
  108. Sequential_string Get_Sequential_Substring(Sequential_string mother_String, int begin_loation, int number)
  109. {
  110. Sequential_string son_String;
  111. int counter;
  112. son_String.length = 0;
  113. if(begin_loation <= 0 || begin_loation > mother_String.length || number < 0 || begin_loation+number-1>mother_String.length)
  114. {
  115. //错误:分割的子字符串的位置错误。
  116. printf("\nError<6>:The position of the divided substring is wrong.\n");
  117. return son_String; // 参数不正确返回空串
  118. }
  119. for(counter = begin_loation-1; counter < begin_loation+number-1; counter++)
  120. {
  121. son_String.Sequential_string_data[counter-begin_loation+1] = mother_String.Sequential_string_data[counter];
  122. }
  123. son_String.Sequential_string_data[counter-begin_loation+1] = '\0';
  124. son_String.length = number;
  125. return son_String;
  126. }
  127. /**************************************************
  128. (7)函数名: Insert_Sequential_String
  129. 功 能: 插入串(从从begin_loation开始插入字符串,然后组合成新的串)
  130. 参 数: (1)Sequential_string old_string:在原始串的基础上插入
  131. (2)int begin_loation: 插入的位置
  132. (3)Sequential_string insert_string:插入的新串
  133. 思 路: 在原有串的基础上,割开一个口子,放上新串,然后组合成新串
  134. 返回值: Sequential_string form_string:组合成的新串
  135. **************************************************/
  136. Sequential_string Insert_Sequential_String(Sequential_string old_string, int begin_loation,Sequential_string insert_string)
  137. {
  138. int counter;
  139. Sequential_string form_string;
  140. form_string.length = 0;
  141. //参数不正确, 返回空串
  142. if(begin_loation <= 0 || begin_loation > old_string.length+1)
  143. {
  144. //错误:插入位置错误
  145. printf("\nError<7>: wrong insertion position.\n");
  146. return form_string;
  147. }
  148. for(counter = 0; counter < begin_loation-1;counter++)
  149. {
  150. form_string.Sequential_string_data[counter] = old_string.Sequential_string_data[counter];
  151. }
  152. for(counter = 0; counter < insert_string.length;counter++)
  153. {
  154. form_string.Sequential_string_data[begin_loation-1+counter] = insert_string.Sequential_string_data[counter];
  155. }
  156. for(counter = begin_loation-1; counter<old_string.length;counter++)
  157. {
  158. form_string.Sequential_string_data[insert_string.length+counter] = old_string.Sequential_string_data[counter];
  159. }
  160. form_string.Sequential_string_data[insert_string.length+counter] = '\0';
  161. form_string.length = old_string.length + insert_string.length;
  162. return form_string;
  163. }
  164. /**************************************************
  165. (8)函数名: Delete_Sequential_String
  166. 功 能: 删除串(从begin 开始的number个字符)
  167. 参 数: (1)Sequential_string old_string:在原有串的基础上删除
  168. (2)int begin_loation: 开始删除的位置(从逻辑1开始)
  169. (3)int number:删除的数量
  170. 注 意: 要判断删除的位置和数量是否正确
  171. 返回值:Sequential_string new_string:删除完后的新串
  172. **************************************************/
  173. Sequential_string Delete_Sequential_String(Sequential_string old_string, int begin_loation,int number)
  174. {
  175. int counter;//定义计数器
  176. Sequential_string new_string;
  177. new_string.length = 0;
  178. //合法性判断(begin_loation理应从1开始到leng长度)
  179. if(begin_loation <= 0 || begin_loation > old_string.length || (begin_loation+number-1) > old_string.length)
  180. {
  181. //错误:删除的位置或数量错误。
  182. printf("Error<8>: Wrong location or quantity of deletion.");
  183. return new_string;//返回空串
  184. }
  185. //择出删除位置之前的串
  186. for(counter = 0; counter < begin_loation-1;counter++)
  187. {
  188. new_string.Sequential_string_data[counter] = old_string.Sequential_string_data[counter];
  189. }
  190. //择出删除位置之后的串
  191. for(counter = begin_loation+number-1; counter < old_string.length; counter++)
  192. {
  193. new_string.Sequential_string_data[counter-number] = old_string.Sequential_string_data[counter];
  194. }
  195. new_string.Sequential_string_data[counter-number] = '\0';
  196. new_string.length = old_string.length - number;
  197. return new_string;
  198. }
  199. /**************************************************
  200. (9)函数名: Replace_Sequential_String
  201. 功 能: 串替换(从begin 开始的number个字符)
  202. 参 数: (1)Sequential_string old_string:原始串
  203. (2)int begin_loation:开始替换的位置
  204. (3)int number:替换的字符个数
  205. (4)Sequential_string replace_string:要替换成的字符串
  206. 思 路: 锁定old_string从begin_loation开始的number个字符,
  207. 然后开始剪切建立新串,
  208. ①把begin_loation之前的字符加入新串,
  209. ②要替换成的串加入,
  210. ③锁定后的字符加入
  211. ④组合成新串,返回传出
  212. 注 意: 最后加'\0'
  213. 返回值: Sequential_string new_string:替换后,产生的新串
  214. **************************************************/
  215. Sequential_string Replace_Sequential_String(Sequential_string old_string, int begin_loation,int number,Sequential_string replace_string)
  216. {
  217. int counter;
  218. Sequential_string new_string;
  219. new_string.length = 0;
  220. //合法性判断
  221. if(begin_loation <= 0 || begin_loation > old_string.length || begin_loation+number-1>old_string.length)
  222. {
  223. //错误:要替换位置出现错误
  224. printf("\nError<9>: There is an error in the position to be replaced.\n");
  225. return new_string;//返回空串
  226. }
  227. //开始复制剪切
  228. for(counter = 0; counter < begin_loation-1; counter++)
  229. {
  230. new_string.Sequential_string_data[counter] = old_string.Sequential_string_data[counter];
  231. }
  232. //加入要替换的串
  233. for(counter = 0; counter < replace_string.length; counter++)
  234. {
  235. new_string.Sequential_string_data[begin_loation-1+counter] = replace_string.Sequential_string_data[counter];
  236. }
  237. //被替换位置,后面剩余的串
  238. for(counter = begin_loation+number-1; counter < old_string.length; counter++)
  239. {
  240. new_string.Sequential_string_data[counter-number+replace_string.length] = old_string.Sequential_string_data[counter];
  241. }
  242. new_string.Sequential_string_data[counter-number+replace_string.length] = '\0';
  243. new_string.length = old_string.length - number + replace_string.length;
  244. return new_string;
  245. }
  246. /**************************************************
  247. (10)函数名: Display_Sequential_String
  248. 功 能: 输出展示串
  249. 参 数: Sequential_string show_String:要输出展示的串
  250. 注 意: 字符串后续可以换成自定义类型
  251. 返回值: 无
  252. **************************************************/
  253. void Display_Sequential_String(Sequential_string show_String)
  254. {
  255. int counter;
  256. if(show_String.length > 0)
  257. {
  258. for(counter = 0; counter < show_String.length; counter++)
  259. {
  260. printf("%c", show_String.Sequential_string_data[counter]);
  261. }
  262. printf("\n");
  263. }
  264. }

main函数调用

  1. int main()
  2. {
  3. //在母串里面找子串
  4. Sequential_string Mother_String;
  5. Sequential_string Son_String;
  6. char Mother_String_array[] = {'a','b','a','b','c','a','b','c','a','c','b','a','b','\0'};
  7. char Son_String_array[] = {'a','b','c','a','c','\0'};
  8. //把两个字符串创建成字符串
  9. Assignment_Sequential_string(Mother_String, Mother_String_array);
  10. Assignment_Sequential_string(Son_String, Son_String_array);
  11. printf("\nMother_String:\n");
  12. Display_Sequential_String(Mother_String);
  13. printf("\nSon_String:\n");
  14. Display_Sequential_String(Son_String);
  15. printf("\n%d\n",Find_matching_location(Mother_String,Son_String));
  16. return 0;
  17. }

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

main.cpp
  1. #include <stdio.h>
  2. #include "Sequential_string.h"
  3. /**************************************************
  4. 函数名: Find_matching_location
  5. 功 能: 利用B-f算法,寻找子串在母串中的位置
  6. 参 数: (1)Sequential_string Mother_String:母串
  7. (2)Sequential_string Son_String:子串
  8. 思 路: 从开始逐个对比,如果对比不成功, 则起始位置前移, 计数器初始化, 直到超出长度或找到
  9. 注 意: 我们判断是否找到的标志,就是判断 子串计数器是否对比完,然后跳出
  10. 返回值: int: -1--没找到, 其他:找到,返回其数组位序
  11. **************************************************/
  12. int Find_matching_location(Sequential_string Mother_String,Sequential_string Son_String)
  13. {
  14. int Mother_counter,Son_counter;//同步计数器
  15. int Mother_begin;//每次对比从母串开始的位置
  16. Mother_counter = 0;
  17. Son_counter = 0;
  18. Mother_counter = 0;
  19. //Brute-Force算法
  20. while((Mother_begin+Mother_counter) < Mother_String.length && Son_counter < Son_String.length)
  21. {
  22. if(Mother_String.Sequential_string_data[Mother_begin+Mother_counter] == Son_String.Sequential_string_data[Son_counter])
  23. {
  24. Mother_counter++;
  25. Son_counter++;
  26. }
  27. else
  28. {
  29. Mother_begin++;
  30. Mother_counter = 0;
  31. Son_counter = 0;
  32. }
  33. }
  34. //通过判断子串计数器的溢出数值,得出是否找到
  35. if(Son_counter >= Son_String.length)
  36. {
  37. return Mother_begin;
  38. }
  39. else
  40. {
  41. return (-1);
  42. }
  43. }
  44. int main()
  45. {
  46. //在母串里面找子串
  47. Sequential_string Mother_String;
  48. Sequential_string Son_String;
  49. char Mother_String_array[] = {'a','b','a','b','c','a','b','c','a','c','b','a','b','\0'};
  50. char Son_String_array[] = {'a','b','c','a','c','\0'};
  51. //把两个字符串创建成字符串
  52. Assignment_Sequential_string(Mother_String, Mother_String_array);
  53. Assignment_Sequential_string(Son_String, Son_String_array);
  54. printf("\nMother_String:\n");
  55. Display_Sequential_String(Mother_String);
  56. printf("\nSon_String:\n");
  57. Display_Sequential_String(Son_String);
  58. printf("\n%d\n",Find_matching_location(Mother_String,Son_String));
  59. return 0;
  60. }

运行结果展示:

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号