BF算法的实现
2016-05-04 20:44
330 查看
#include<iostream>
#include<string.h>
using namespace std;
typedef struct
{
char ch[50];
int length;
}SString;
int Index_BF(SString S,SString T,int pos)
{
cout<<"请输入S字符串:"<<endl;
cin>>S.ch;
cout<<"请输入T字符串:"<<endl;
cin>>T.ch;
cout<<"请输入主串中开始进行匹配的位置(首字符位置为1):";
cin>>pos;
S.length=strlen(S.ch);
T.length=strlen(T.ch);
int i=pos,j=1;
while(i<=S.length&&j<=T.length)
{
if(S.ch[i]==T.ch[j])
{
++i;
++j;
}
else
{
i=i-j+2;
j=1;
}
}
if(j>T.length)
return i-T.length;
else return 0;
}
int main()
{
SString S,T;
char ch;
int pos;
cout<<"是否进行测试(输入y或Y继续,任意其他键结束):";
cin>>ch;
while(ch=='y'||ch=='Y')
{
int result=Index_BF(S,T,pos);
if(result!=0)
cout<<"主串与子串在主串的第"<<result<<"个字符处首次匹配"<<endl;
else
cout<<"匹配失败"<<endl;
cout<<"是否继续测试(输入y或Y继续,任意其他键结束):";
cin>>ch;
}
}
#include<string.h>
using namespace std;
typedef struct
{
char ch[50];
int length;
}SString;
int Index_BF(SString S,SString T,int pos)
{
cout<<"请输入S字符串:"<<endl;
cin>>S.ch;
cout<<"请输入T字符串:"<<endl;
cin>>T.ch;
cout<<"请输入主串中开始进行匹配的位置(首字符位置为1):";
cin>>pos;
S.length=strlen(S.ch);
T.length=strlen(T.ch);
int i=pos,j=1;
while(i<=S.length&&j<=T.length)
{
if(S.ch[i]==T.ch[j])
{
++i;
++j;
}
else
{
i=i-j+2;
j=1;
}
}
if(j>T.length)
return i-T.length;
else return 0;
}
int main()
{
SString S,T;
char ch;
int pos;
cout<<"是否进行测试(输入y或Y继续,任意其他键结束):";
cin>>ch;
while(ch=='y'||ch=='Y')
{
int result=Index_BF(S,T,pos);
if(result!=0)
cout<<"主串与子串在主串的第"<<result<<"个字符处首次匹配"<<endl;
else
cout<<"匹配失败"<<endl;
cout<<"是否继续测试(输入y或Y继续,任意其他键结束):";
cin>>ch;
}
}
相关文章推荐
- KMP算法的实现
- 数据结构(1)队列
- 数据结构 二叉树
- 数据结构之表
- 数据结构实验之链表二:逆序建立链表
- 数据结构实验之链表一:顺序建立链表
- 数据结构(栈,队列,链表,二叉树)
- 数据结构学习笔记06排序 (冒泡、插入、希尔、堆排序、归并排序)
- 数据结构(11):图的存储方式(邻接矩阵、邻接表....)
- Java数据结构 -ArrayDeque 双端队列的简单分析
- 【数据结构与算法】B/B+ 树 、RB树
- 数据结构与算法--初探门径
- 数据结构与算法——最短路径Dijkstra算法的C++实现
- 数据结构与算法——最短路径Dijkstra算法的C++实现
- 数据结构笔记整理第3章:栈和队列
- 基础数据结构之栈和队列
- [python]常见数据结构归纳
- 数据结构第九章[查找]1
- 102TimeStatistic
- 101Triplet