BF算法和 KMP 算法
来源:互联网 发布:兽药尼蒙尔克素淘宝网 编辑:程序博客网 时间:2024/06/11 12:01
这几天学习了神奇的字符串匹配算法,
在没学之前,按照自己的想法写出了BF算法:
#include <stdio.h>#include <string.h>char ch[1001],p[12];int lenth_ch,lenth_p;int BF(){ int i=0,j=0,counter=0; while(i<lenth_ch) { while(i<lenth_ch&&j<lenth_p) { if(ch[i]==p[j]) { i++;j++; } else { i=i-j+1;j=0;} } if(j==lenth_p) counter++; i=i-j+1;j=0;g } return counter;}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%s",p); scanf("%s",ch); lenth_p=strlen(p); lenth_ch=strlen(ch); printf("%d\n",BF()); } return 0;}
学了数据结构之后:#include <stdio.h>#include <string.h>#define M 200void getnext(char t[],int length,int *next){ int i=0,j=-1; next[0]=-1; while(i<length-1) { if(j==-1||t[i]==t[j]) { i++; j++; if(t[i]!=t[j]) next[i]=j; else next[i]=next[j]; } else j=next[j]; }}int KMP(char *s,char *t,int pos){ int i=pos,j=0,next[M]; getnext(t,strlen(t),next); while(i<(int)strlen(s) && j<(int)strlen(t)) { if(j==-1||s[i]==t[j]) { i++; j++; } else { j=next[j]; } } if(j==(int)strlen(t)) return i-j;//返回开始匹配到的主串位置 else return -1;}int main(){ char a[M],b[M]; while(~scanf("%s%s",a,b)) { if(KMP(a,b,0)!==-1) printf("found\n"); else printf("not found\n"); return 0;}
- BF和KMP算法
- BF和KMP算法
- BF算法和KMP算法
- BF算法和 KMP 算法
- BF算法和KMP算法
- 【字符 BF 和 KMP算法】
- 字符串--BF和KMP算法
- C++ BF和KMP算法
- C++实现:BF算法和KMP算法
- BF算法和KMP算法对比
- 字符串匹配BF算法和KMP算法
- BF算法和KMP算法的简介
- 第五章 BF算法和KMP算法
- BF与KMP算法
- KMP算法 bf
- Kmp 与 BF算法
- hdu 2087 BF 和 KMP算法
- 字符串匹配算法(BF和KMP)
- phpweb 网站模版系统文件系统结构说明
- uint8_t / uint16_t / uint32_t /uint64_t 的简单介绍
- 零起步7-CentOS6.3关于LAMP的配置apache-2.4.3、php-5.4.7、phpMyAdmin3.5
- phpweb 开发规范和基本要求
- phpweb 怎样制作模板、发布模板资源
- BF算法和 KMP 算法
- C#中sealed关键字的作用
- 如何制作标签切换边框模版 phpweb
- http://acm.hdu.edu.cn/showproblem.php?pid=2841 题意很简单
- phpweb 怎样开发插件、发布插件资源
- 关于C++ const 的全面总结
- 零起步8-CentOS6.3安装配置mongodb-2.2、rockmongo
- phpweb 怎样在系统中增加自定义功能页
- phpweb 网站 -会员模块安装后会员功能菜单修复方法