├── README.md ├── my_image ├── run_0.png └── run_1.png ├── search_string.c ├── search_string.h ├── search_string.o ├── search_test └── search_test.c /README.md: -------------------------------------------------------------------------------- 1 | # string_match 2 | ## 对比了各类字符串匹配算法 3 | 4 | * string_match 5 | * Created on: 2018-7-19 6 | * Author: MK 7 | * usage:四种字符串匹配算法的实现(Sunday、KMP、Boyer-Moore、horspool)的测试 8 | 9 | ### 各文件说明: 10 | 11 | search_string.h 头文件,包含了对各个函数的声明; 12 | search_string.c 包含了头文件中所有函数的具体实现; 13 | search_string.o 由search_string.c编译得到(命令:gcc -c search_string.c) 14 | search_test.c 主函数,即测试 15 | search_test linux下可执行程序,即最终的测试程序,由search_test.c编译链接而得(命令:gcc -o search_test search_test.c search_string.o) 16 | 17 | ### 说明: 18 | 本测试是在centos6.4下进行的实验,运行时的截图在文件夹my_image里,可供使用者参考; 19 | 20 | ### 使用指导: 21 | #### (1)、可直接在Linux下运行search_test(命令:./search_test )进行测试; 22 | #### (2)、也可自行修改关键字符串和文本串来进行自己的测试,修改步骤如下: 23 | 24 | a、在search_string.h中将My_TXT修改成自己的文本串(注意换行问题); 25 | b、在search_test.c中将pattern修改成自己的关键字符串; 26 | c、编译链接(具体命令在下一步); 27 | #### (3)、自行编译的命令步骤: 28 | 29 | a、将所有文件放在同一目录下,通过命令行进入该目录,先将search_string.c编译为search_string.o(命令:gcc -c search_string.c)(若未修改search_string.c的内容,这一步可不用,可直接使用所给的search_string.o); 30 | b、上一步完成后,输入(命令:gcc -o search_test search_test.c search_string.o); 31 | c、完成; 32 | 33 | [我的博文](https://blog.csdn.net/qq_33515733/article/details/81163135) 34 | -------------------------------------------------------------------------------- /my_image/run_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NewBee119/string_match/b053ac9e0984bf3bd86f04196fa750515b844170/my_image/run_0.png -------------------------------------------------------------------------------- /my_image/run_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NewBee119/string_match/b053ac9e0984bf3bd86f04196fa750515b844170/my_image/run_1.png -------------------------------------------------------------------------------- /search_string.c: -------------------------------------------------------------------------------- 1 | /* 2 | * search_string.c 3 | * Created on: 2018-7-19 4 | * Author: MK 5 | * usage:四种字符串匹配算法的实现(Sunday、KMP、Boyer-Moore、horspool) 6 | */ 7 | #include 8 | #include 9 | #define maxNum 256 10 | #define MAX(x, y) (x)>(y) ? (x):(y) 11 | //////////////////////////Sunday////////////////////////////////////// 12 | void SundayPre(int sun_shift[],char* P,int lenP) 13 | { 14 | int i; 15 | for(i=0;i=lenP){ 46 | //printf("Sunday Find it. the position is %d jump %d time\n",pos,count); 47 | return &T[pos]; 48 | } 49 | else{ 50 | // 找到主串中当前跟模式串匹配的最末字符的下一个字符 51 | // 在模式串中出现最后的位置 52 | // 所需要从(模式串末尾+1)移动到该位置的步数 53 | //printf("Sunday current step: %d jump num: %d\n",count++,shift[T[pos+lenP]]); 54 | pos+=shift[T[pos+lenP]]; 55 | //count++; 56 | //printf("pos %d\n",pos); 57 | } 58 | } 59 | //printf("Sunday not Find. jump %d time\n",count); 60 | return NULL; 61 | } 62 | 63 | ////////////////////////////////KMP////////////////////////////////////////////// 64 | //优化过后的next 数组求法 65 | /* 66 | function: 67 | KMP的next数组求解(预处理) 68 | Param: 69 | @p 需要匹配的字符串 70 | @next 需要匹配的字符串对应的next数组 71 | */ 72 | void KMPPre(char* p, int KMP_next[]) 73 | { 74 | int pLen=strlen(p); 75 | KMP_next[0]=-1; 76 | int k=-1; 77 | int j=0; 78 | while(j=0;i--){ 161 | j = i; 162 | while(j>=0&&pattern[j]==pattern[m-1-i+j]) j--; 163 | suff[i]=i-j; 164 | } 165 | } 166 | /* 167 | function: 168 | 新版的好后缀辅助数组(好后缀长度)求解方法 169 | Param: 170 | @pattern 需要匹配的字符串 171 | @suff 好后缀辅助数组 172 | @m 需要匹配的字符串长度 173 | */ 174 | void suffix(char *pattern,int m,int suff[]) { 175 | int f, g, i; 176 | suff[m-1]=m; 177 | g=m-1; 178 | for(i=m-2;i>=0;--i){ 179 | if(i>g&&suff[i+m-1-f]=0&&pattern[g]==pattern[g+m-1-f]) 186 | --g; 187 | suff[i]=f-g; 188 | } 189 | } 190 | } 191 | /* 192 | function: 193 | 好后缀数组求解方法 194 | Param: 195 | @pattern 需要匹配的字符串 196 | @bmGs 好后缀数组 197 | @m 需要匹配的字符串长度 198 | */ 199 | void PreBmGs(char *pattern,int m,int bmGs[]) 200 | { 201 | int i, j; 202 | int suff[maxNum]; 203 | // 计算后缀数组 204 | suffix(pattern,m,suff); 205 | // 先全部赋值为m,包含Case3 206 | for(i=0;i=0;i--){ 212 | if(suff[i]==i+1){ 213 | for(;j=0&&pattern[i]==text[i+pos];i--); 240 | if(i < 0){ 241 | return &text[pos]; 242 | } 243 | else{ 244 | pos+=MAX(bmBc[text[i+pos]]-m+1+i,bmGs[i]); 245 | } 246 | } 247 | return NULL; 248 | } 249 | //////////////////////////horspool//////////////////////////////// 250 | /* 251 | function:horspool字符匹配算预处理 252 | Param: 253 | @P 需要匹配的字符串 254 | @lenP 需要匹配的字符串长度 255 | */ 256 | void horspoolPre(int hors_d[],char* P,int lenP) 257 | { 258 | int i; 259 | for(i=0;i=0&&T[pos+j]==P[j]) j--; 282 | if(j==-1){ 283 | return &T[pos]; 284 | } 285 | else{ 286 | pos+=hors_d[T[pos+lenP-1]]; 287 | } 288 | } 289 | return NULL; 290 | } -------------------------------------------------------------------------------- /search_string.h: -------------------------------------------------------------------------------- 1 | /* 2 | * search_string.h 3 | * Created on: 2018-7-19 4 | * Author: MK 5 | * usage:四种字符串匹配算法的实现(Sunday、KMP、Boyer-Moore、horspool)的声明 6 | */ 7 | #ifndef __SEARCH_CHA__ 8 | #define __SEARCH_CHA__ 9 | #include 10 | #include 11 | //匹配的文本内容 在里面填上自己的特定字符MY_TEST_string 12 | char* My_TXT="LOCALGX=%u6210%u90FD%7C%36%36%39%33%7C%u6210%u90FD%7C%36%36%39%33; Hm_lvt_e9e114d958ea263de46e080563e254c4=1531471439,1531472197," 13 | "1531537654,1531618652; BAIDUID=3ACF228CE89D0C2D28B1B3DA5C56C138:FG=1; Hm_lpvt_e9e114d958ea263de46e080563e254c4=1531629911;" 14 | "BIDUPSID=C18478BCF075BCE0D2D0149D77980B8B; PSTM=1531620860; H_PS_PSSID=26523_1451_25810_21106_22159 Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12)" 15 | " Gecko/20130109 Firefox/10.0.12 http://news.baidu.com/mil AndroidDownloadManager/7.1.2 (Linux; U; Android 7.1.2; MI 5X Build/N2G47H) http://switch.pcfg.cache." 16 | "wpscdn.cn/wps_assets/cfg/ad/switch/load_bubblehttp://notify.wps.cn/notifyserver/notify?v=qidVrBsEjwqbMd%2F4ydmZ5CRKfbUlVq11EGkClTHeWH%2B5msXpw%2BQJC0oEFK%2FB8vu2bNj" 17 | "aDZpcjl1wviBMWPHuYA== http://tile-service.weather.microsoft.com/zh-CN/livetile/preinstall?region=CN&appid=C98EA5B0842DBB9405BBF071E1DA76512D21FE36&FORM=Threshold" 18 | "http://res.res.res.res:80/" 19 | "BIDUPSID=C18478BCF075BCE0D2D0149D77980B8B; PSTM=1531620860; H_PS_PSSID=26523_1451_25810_21106_22159 Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12)" 20 | " Gecko/20130109 Firefox/10.0.12 http://news.baidu.com/mil AndroidDownloadManager/7.1.2 (Linux; U; Android 7.1.2; MI 5X Build/N2G47H) http://switch.pcfg.cache." 21 | "wpscdn.cn/wps_assets/cfg/ad/switch/load_bubblehttp://notify.wps.cn/notifyserver/notify?v=qidVrBsEjwqbMd%2F4ydmZ5CRKfbUlVq11EGkClTHeWH%2B5msXpw%2BQJC0oEFK%2FB8vu2bNj" 22 | "aDZpcjl1wviBMWPHuYA== http://tile-service.weather.microsoft.com/zh-CN/livetile/preinstall?region=CN&appid=C98EA5B0842DBB9405BBF071E1DA76512D21FE36&FORM=Threshold" 23 | "http://res.res.res.res:80/" 24 | "BIDUPSID=C18478BCF075BCE0D2D0149D77980B8B; PSTM=1531620860; H_PS_PSSID=26523_1451_25810_21106_22159 Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12)" 25 | " Gecko/20130109 Firefox/10.0.12 http://news.baidu.com/mil AndroidDownloadManager/7.1.2 (Linux; U; Android 7.1.2; MI 5X Build/N2G47H) http://switch.pcfg.cache." 26 | "wpscdn.cn/wps_assets/cfg/ad/switch/load_bubblehttp://notify.wps.cn/notifyserver/notify?v=qidVrBsEjwqbMd%2F4ydmZ5CRKfbUlVq11EGkClTHeWH%2B5msXpw%2BQJC0oEFK%2FB8vu2bNj" 27 | "aDZpcjl1wviBMWPHuYA== http://tile-service.weather.microsoft.com/zh-CN/livetile/preinstall?region=CN&appid=C98EA5B0842DBB9405BBF071E1DA76512D21FE36&FORM=Threshold" 28 | "http://res.res.res.res:80/" 29 | "BIDUPSID=C18478BCF075BCE0D2D0149D77980B8B; PSTM=1531620860;MY_TEST_string H_PS_PSSID=26523_1451_25810_21106_22159 Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12)" 30 | " Gecko/20130109 Firefox/10.0.12 http://news.baidu.com/mil AndroidDownloadManager/7.1.2 (Linux; U; Android 7.1.2; MI 5X Build/N2G47H) http://switch.pcfg.cache." 31 | "wpscdn.cn/wps_assets/cfg/ad/switch/load_bubblehttp://notify.wps.cn/notifyserver/notify?v=qidVrBsEjwqbMd%2F4ydmZ5CRKfbUlVq11EGkClTHeWH%2B5msXpw%2BQJC0oEFK%2FB8vu2bNj" 32 | "aDZpcjl1wviBMWPHuYA== http://tile-service.weather.microsoft.com/zh-CN/livetile/preinstall?region=CN&appid=C98EA5B0842DBB9405BBF071E1DA76512D21FE36&FORM=Threshold" 33 | "http://res.res.res.res:80/" 34 | "BIDUPSID=C18478BCF075BCE0D2D0149D77980B8B; PSTM=1531620860; H_PS_PSSID=26523_1451_25810_21106_22159 Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12)" 35 | " Gecko/20130109 Firefox/10.0.12 http://news.baidu.com/mil AndroidDownloadManager/7.1.2 (Linux; U; Android 7.1.2; MI 5X Build/N2G47H) http://switch.pcfg.cache." 36 | "wpscdn.cn/wps_assets/cfg/ad/switch/load_bubblehttp://notify.wps.cn/notifyserver/notify?v=qidVrBsEjwqbMd%2F4ydmZ5CRKfbUlVq11EGkClTHeWH%2B5msXpw%2BQJC0oEFK%2FB8vu2bNj" 37 | "aDZpcjl1wviBMWPHuYA== http://tile-service.weather.microsoft.com/zh-CN/livetile/preinstall?region=CN&appid=C98EA5B0842DBB9405BBF071E1DA76512D21FE36&FORM=Threshold" 38 | "http://res.res.res.res:80/" 39 | "BIDUPSID=C18478BCF075BCE0D2D0149D77980B8B; PSTM=1531620860; H_PS_PSSID=26523_1451_25810_21106_22159 Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12)" 40 | " Gecko/20130109 Firefox/10.0.12 http://news.baidu.com/mil AndroidDownloadManager/7.1.2 (Linux; U; Android 7.1.2; MI 5X Build/N2G47H) http://switch.pcfg.cache." 41 | "wpscdn.cn/wps_assets/cfg/ad/switch/load_bubblehttp://notify.wps.cn/notifyserver/notify?v=qidVrBsEjwqbMd%2F4ydmZ5CRKfbUlVq11EGkClTHeWH%2B5msXpw%2BQJC0oEFK%2FB8vu2bNj" 42 | "aDZpcjl1wviBMWPHuYA== http://tile-service.weather.microsoft.com/zh-CN/livetile/preinstall?region=CN&appid=C98EA5B0842DBB9405BBF071E1DA76512D21FE36&FORM=Threshold" 43 | "http://res.res.res.res:80/" 44 | "BIDUPSID=C18478BCF075BCE0D2D0149D77980B8B; PSTM=1531620860; MY_TEST_MYringH_PS_PSSID=26523_1451_25810_21106_22159 Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12)" 45 | " Gecko/20130109 Firefox/10.0.12 http://news.baidu.com/mil AndroidDownloadManager/7.1.2 (Linux; U; Android 7.1.2; MI 5X Build/N2G47H) http://switch.pcfg.cache." 46 | "wpscdn.cn/wps_assets/cfg/ad/switch/load_bubblehttp://notify.wps.cn/notifyserver/notify?v=qidVrBsEjwqbMd%2F4ydmZ5CRKfbUlVq11EGkClTHeWH%2B5msXpw%2BQJC0oEFK%2FB8vu2bNj" 47 | "aDZpcjl1wviBMWPHuYA== http://tile-service.weather.microsoft.com/zh-CN/livetile/preinstall?region=CN&appid=C98EA5B0842DBB9405BBF071E1DA76512D21FE36&FORM=Threshold" 48 | "http://res.res.res.res:80/" 49 | "BIDUPSID=C18478BCF075BCE0D2D0149D77980B8B; PSTM=1531620860; H_PS_PSSID=26523_1451_25810_21106_22159 Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12)" 50 | " Gecko/20130109 Firefox/10.0.12 http://news.baidu.com/mil AndroidDownloadManager/7.1.2 (Linux; U; Android 7.1.2; MI 5X Build/N2G47H) http://switch.pcfg.cache." 51 | "wpscdn.cn/wps_assets/cfg/ad/switch/load_bubblehttp://notify.wps.cn/notifyserver/notify?v=qidVrBsEjwqbMd%2F4ydmZ5CRKfbUlVq11EGkClTHeWH%2B5msXpw%2BQJC0oEFK%2FB8vu2bNj" 52 | "aDZpcjl1wviBMWPHuYA== http://tile-service.weather.microsoft.com/zh-CN/livetile/preinstall?region=CN&appid=C98EA5B0842DBB9405BBF071E1DA76512D21FE36&FORM=Threshold" 53 | "http://res.res.res.res:80/" 54 | "BIDUPSID=C18478BCF075BCE0D2D0149D77980B8B; PSTM=1531620860; H_PS_PSSID=26523_1451_25810_21106_22159 Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12)" 55 | " Gecko/20130109 Firefox/10.0.12 http://news.baidu.com/mil AndroidDownloadManager/7.1.2 (Linux; U; Android 7.1.2; MI 5X Build/N2G47H) http://switch.pcfg.cache." 56 | "wpscdn.cn/wps_assets/cfg/ad/switch/load_bubblehttp://notify.wps.cn/notifyserver/notify?v=qidVrBsEjwqbMd%2F4ydmZ5CRKfbUlVq11EGkClTHeWH%2B5msXpw%2BQJC0oEFK%2FB8vu2bNj" 57 | "aDZpcjl1wviBMWPHuYA== http://tile-service.weather.microsoft.com/zh-CN/livetile/preinstall?region=CN&appid=C98EA5B0842DBB9405BBF071E1DA76512D21FE36&FORM=Threshold" 58 | "http://res.res.res.res:80/" 59 | "BIDUPSID=C18478BCF075BCE0D2D0149D77980B8B; PSTM=1531620860; H_PS_PSSID=26523_1451_25810_21106_22159 Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12)" 60 | " Gecko/20130109 Firefox/10.0.12 http://news.baidu.com/mil AndroidDownloadManager/7.1.2 (Linux; U; Android 7.1.2; MI 5X Build/N2G47H) http://switch.pcfg.cache." 61 | "wpscdn.cn/wps_assets/cfg/ad/switch/load_bubblehttp://notify.wps.cn/notifyserver/notify?v=qidVrBsEjwqbMd%2F4ydmZ5CRKfbUlVq11EGkClTHeWH%2B5msXpw%2BQJC0oEFK%2FB8vu2bNj" 62 | "aDZpcjl1wviBMWPHuYA== http://tile-service.weather.microsoft.com/zh-CN/livetile/preinstall?region=CN&appid=C98EA5B0842DBB9405BBF071E1DA76512D21FE36&FORM=Threshold" 63 | "http://res.res.res.res:80/" 64 | "BIDUPSID=C18478BCF075BCE0D2D0149D77980B8B; PSTM=1531620860; H_PS_PSSID=26523_1451_25810_21106_22159 Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12)" 65 | " Gecko/20130109 Firefox/10.0.12 http://news.baidu.com/mil AndroidDownloadManager/7.1.2 (Linux; U; Android 7.1.2; MI 5X Build/N2G47H) http://switch.pcfg.cache." 66 | "wpscdn.cn/wps_assets/cfg/ad/switch/load_bubblehttp://notify.wps.cn/notifyserver/notify?v=qidVrBsEjwqbMd%2F4ydmZ5CRKfbUlVq11EGkClTHeWH%2B5msXpw%2BQJC0oEFK%2FB8vu2bNj" 67 | "aDZpcjl1wviBMWPHuYA== http://tile-service.weather.microsoft.com/zh-CN/livetile/preinstall?region=CN&appid=C98EA5B0842DBB9405BBF071E1DA76512D21FE36&FORM=Threshold" 68 | "http://res.res.res.res:80/" "BIDUPSID=C18478BCF075BCE0D2D0149D77980B8B; PSTM=1531620860; H_PS_PSSID=26523_1451_25810_21106_22159 Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12)" 69 | " Gecko/20130109 Firefox/10.0.12 http://news.baidu.com/mil AndroidDownloadManager/7.1.2 (Linux; U; Android 7.1.2; MI 5X Build/N2G47H) http://switch.pcfg.cache." 70 | "wpscdn.cn/wps_assets/cfg/ad/switch/load_bubblehttp://notify.wps.cn/notifyserver/notify?v=qidVrBsEjwqbMd%2F4ydmZ5CRKfbUlVq11EGkClTHeWH%2B5msXpw%2BQJC0oEFK%2FB8vu2bNj" 71 | "aDZpcjl1wviBMWPHuYA== http://tile-service.weather.microsoft.com/zh-CN/livetile/preinstall?region=CN&appid=C98EA5B0842DBB9405BBF071E1DA76512D21FE36&FORM=Threshold" 72 | "http://res.res.res.res:80/" "BIDUPSID=C18478BCF075BCE0D2D0149D77980B8B; PSTM=1531620860; H_PS_PSSID=26523_1451_25810_21106_22159 Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12)" 73 | " Gecko/20130109 Firefox/10.0.12 http://news.baidu.com/mil AndroidDownloadManager/7.1.2 (Linux; U; Android 7.1.2; MI 5X Build/N2G47H) http://switch.pcfg.cache." 74 | "wpscdn.cn/wps_assets/cfg/ad/switch/load_bubblehttp://notify.wps.cn/notifyserver/notify?v=qidVrBsEjwqbMd%2F4ydmZ5CRKfbUlVq11EGkClTHeWH%2B5msXpw%2BQJC0oEFK%2FB8vu2bNj" 75 | "aDZpcjl1wviBMWPHuYA== http://tile-service.weather.microsoft.com/zh-CN/livetile/preinstall?region=CN&appid=C98EA5B0842DBB9405BBF071E1DA76512D21FE36&FORM=Threshold" 76 | "http://res.res.res.res:80/" "BIDUPSID=C18478BCF075BCE0D2D0149D77980B8B; PSTM=1531620860; H_PS_PSSID=26523_1451_25810_21106_22159 Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12)" 77 | " Gecko/20130109 Firefox/10.0.12 http://news.baidu.com/mil AndroidDownloadManager/7.1.2 (Linux; U; Android 7.1.2; MI 5X Build/N2G47H) http://switch.pcfg.cache." 78 | "wpscdn.cn/wps_assets/cfg/ad/switch/load_bubblehttp://notify.wps.cn/notifyserver/notify?v=qidVrBsEjwqbMd%2F4ydmZ5CRKfbUlVq11EGkClTHeWH%2B5msXpw%2BQJC0oEFK%2FB8vu2bNj" 79 | "aDZpcjl1wviBMWPHuYA== http://tile-service.weather.microsoft.com/zh-CN/livetile/preinstall?region=CN&appid=C98EA5B0842DBB9405BBF071E1DA76512D21FE36&FORM=Threshold" 80 | "http://res.res.res.res:80/"; 81 | 82 | //预处理函数声明 83 | void SundayPre(int sun_shift[],char* P,int lenP); 84 | void KMPPre(char* p, int KMP_next[]); 85 | void PreBmBc(char *pattern,int m,int bmBc[]); 86 | void PreBmGs(char *pattern,int m,int bmGs[]); 87 | void horspoolPre(int hors_d[],char* P,int lenP); 88 | /* 89 | function: 90 | Sunday字符匹配算法 91 | Param: 92 | @T 文本内容 93 | @TLen 文本内容长度 94 | @p 需要匹配的字符串 95 | @lenP 需要匹配的字符串长度 96 | */ 97 | char* Sunday(char* T,int lenT,char* P,int lenP,int shift[]); 98 | /* 99 | function: 100 | KMP字符匹配算法 101 | Param: 102 | @s 文本内容 103 | @sLen 文本内容长度 104 | @p 需要匹配的字符串 105 | @pLen 需要匹配的字符串长度 106 | */ 107 | char* KmpSearch(char* s,int sLen,char* p,int pLen,int next[]); 108 | /* 109 | function: 110 | Boyer-Moore字符匹配算法 111 | Param: 112 | @text 文本内容 113 | @n 文本内容长度 114 | @pattern 需要匹配的字符串 115 | @m 需要匹配的字符串长度 116 | */ 117 | char* BoyerMoore(char *text,int n,char *pattern,int m,int bmBc[],int bmGs[]); 118 | /* 119 | function: 120 | horspool字符匹配算法 121 | Param: 122 | @T 文本内容 123 | @lenT 文本内容长度 124 | @P 需要匹配的字符串 125 | @lenP 需要匹配的字符串长度 126 | */ 127 | char* horspool(char *T, int lenT, char *P, int lenP,int hors_d[]); 128 | #endif -------------------------------------------------------------------------------- /search_string.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NewBee119/string_match/b053ac9e0984bf3bd86f04196fa750515b844170/search_string.o -------------------------------------------------------------------------------- /search_test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NewBee119/string_match/b053ac9e0984bf3bd86f04196fa750515b844170/search_test -------------------------------------------------------------------------------- /search_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * search_test.c 3 | * Created on: 2018-7-19 4 | * Author: MK 5 | * usage:四种字符串匹配算法的实现(Sunday、KMP、Boyer-Moore、horspool)的测试 6 | */ 7 | #include "search_string.h" 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #define My_TXTMAX 1000 14 | #define times 1 //运行次数 15 | #define MAXCH 256 16 | int main() 17 | { //初始化一些变量 18 | char* pattern="MY_TEST_string";//模式串 19 | int lenp,len_txt; 20 | lenp=strlen(pattern); 21 | len_txt=strlen(My_TXT);//My_TXT为文本串,具体内容在"search_character.h"中 22 | int sun_shift[MAXCH];//Sunday辅助数组 23 | int KMP_next[lenp];//KMP辅助数组 24 | int bmBc[MAXCH];//BM坏字符数组 25 | int bmGs[lenp];//BM好后缀数组 26 | int hors_d[MAXCH];//horspool辅助数组 27 | struct timeval start,end;//用来记录时间 28 | long dif_sec, dif_usec;//存储时间差 29 | int i; 30 | char *temp[5],test[5][256]; 31 | //四种算法的预处理工作 32 | SundayPre(sun_shift,pattern,lenp); 33 | KMPPre(pattern,KMP_next); 34 | PreBmBc(pattern,lenp,bmBc); 35 | PreBmGs(pattern,lenp,bmGs); 36 | horspoolPre(hors_d,pattern,lenp); 37 | //测试开始 38 | ///////////KmpSearch/////////// 39 | gettimeofday(&start,NULL);//起始时间 40 | for(i=0;i