博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
几个用C++写的字符串搜索代码
阅读量:6957 次
发布时间:2019-06-27

本文共 4026 字,大约阅读时间需要 13 分钟。

hot3.png

1.关于本文

本文中所有的代码都为实现一个目标,即:

写一个函数:从字符串s中找到字符串a第一次出现的位置,不存在则返回-1

2.方法1:朴素的字符串搜索算法

用一个循环来找出所有有效位移

#include
#include
using namespace std;//函数:找出字符串s中第一次出现字符串a的位置(朴素算法)int IndexOf_01(string s,string a){ bool isMatch; for(int i = 0; i <= s.length() - a.length(); i++) { isMatch = true; for(int j = 0; j < a.length(); j++) { if(s[i + j] != a[j]) { isMatch = false; break; } } if(isMatch) { return i; } } return -1;}int main(){ string s = "the quick brown fox jumped over the lazy dogs"; cout << s << endl; cout << "index of \"the\": " << IndexOf_01(s, "the") << endl; cout << "index of \"quick\": " << IndexOf_01(s, "quick") << endl; cout << "index of \"dogs\": " << IndexOf_01(s, "dogs") << endl; cout << "index of \"cats\": " << IndexOf_01(s, "cats") << endl; return 0;}

 

152221_VACB_1425762.png

3.方法2

本方法受到了 Rabin-Karp 启发,相当于对原算法做了如下修改

窗口长度:m=a.length(),第一次匹配规则:t=t[0]+t[1]+...+t[t.length()-1],模p=max

对于字符串s的每一个长度为m的子串,如果该子串所有字符的ASCII码和与目标字符串相同,则逐个比对该子串与目标字符串是否相同,否则跳过。若子串与目标字符串相同,则认为匹配,否则继续向后搜索。

#include
#include
using namespace std;//函数:找出字符串s中第一次出现字符串a的位置(受到Rabin-Karp算法启发)int IndexOf_02(string s,string a){ //s的长度应大于a的长度 if(s.length() < a.length()) { return -1; } //计算第一组数字和 int target = 0, current = 0; for(int i = 0; i < a.length(); i++) { target += a[i]; current += s[i]; } //开始匹配 bool isMatch; for(int i = 0; i <= s.length() - a.length(); ) { //如果所有字符的数字和相同,则进一步比较 if(current == target) { isMatch = true; for(int j = 0; j < a.length(); j++) { if(s[i + j] != a[j]) { isMatch = false; break; } } if(isMatch) { return i; } } //计算下一组字符的数字和 current -= s[i]; current += s[i++ + a.length()]; } return -1;}int main(){ string s = "the quick brown fox jumped over the lazy dogs"; cout << s << endl; cout << "index of \"the\": " << IndexOf_02(s, "the") << endl; cout << "index of \"quick\": " << IndexOf_02(s, "quick") << endl; cout << "index of \"dogs\": " << IndexOf_02(s, "dogs") << endl; cout << "index of \"cats\": " << IndexOf_02(s, "cats") << endl; return 0;}

152142_Q2zm_1425762.png

4.Knuth-Morris-Pratt(KMP)算法

KMP算法,函数ComputePrefix用于计算辅助数组

#include
#include
using namespace std;//计算前缀int* ComputePrefix(string a){ int *array = new int[a.length() + 1]; int x = 0; array[0] = 0; for(int i = 1; i < a.length(); i++) { if(a[i] == a[x]) { array[i] = array[i - 1] + 1; x++; } else { array[i] = 0; x = 0; } } return array;}//函数:找出字符串s中第一次出现字符串a的位置(KMP算法)int IndexOf_KMP(string s, string a){ //计算前缀 int *perfix = new int[a.length()]; perfix = ComputePrefix(a); //判断a在s中第一次出现的位置 int x = 0, y = 0; while(x < s.length()) { if(s[x + y] == a[y]) { y++; if(y == a.length()) { return x; } } else { if(y == 0) { x++; } else { x += (y - perfix[y - 1]); } y = 0; } } return -1;}int main(){ string tests = "ababaabababcabab"; cout << tests << endl; string testa0 = "ababa"; cout << testa0 << ": " << IndexOf_KMP(tests, testa0) << endl; string testa1 = "ababc"; cout << testa1 << ": " << IndexOf_KMP(tests, testa1) << endl; string testa2 = "cabab"; cout << testa2 << ": " << IndexOf_KMP(tests, testa2) << endl; string testa3 = "aaaaa"; cout << testa3 << ": " << IndexOf_KMP(tests, testa3) << endl; return 0;}

152928_hiej_1425762.png

转载于:https://my.oschina.net/Tsybius2014/blog/272355

你可能感兴趣的文章
DotImage使用教程:从数据库中读写图像
查看>>
锐捷CCNA系列(五) 交换机配置模式切换
查看>>
ffmpeg的使用
查看>>
【最小割】【Dinic】bzoj3275 Number
查看>>
PHP RSA加解密示例(转)
查看>>
Django权限1
查看>>
Python调用 c++ dll,并且使用Py2exe打包
查看>>
安卓布局随记
查看>>
使用gearman进行异步的邮件或短信发送
查看>>
XGBoost:在Python中使用XGBoost
查看>>
python基础知识~ 序列化
查看>>
Activity的启动模式(android:launchMode)
查看>>
java设计模式演示样例
查看>>
创建与删除索引
查看>>
HTML5新增核心工具——canvas
查看>>
改动file header (測)
查看>>
微软职位内部推荐-Senior Speech TTS
查看>>
UVA - 10574 Counting Rectangles
查看>>
HDU3336-Count the string(KMP)
查看>>
常用API接口签名验证参考
查看>>