数据结构 串与模式匹配Word文档格式.docx

上传人:b****6 文档编号:16696920 上传时间:2022-11-25 格式:DOCX 页数:13 大小:71.80KB
下载 相关 举报
数据结构 串与模式匹配Word文档格式.docx_第1页
第1页 / 共13页
数据结构 串与模式匹配Word文档格式.docx_第2页
第2页 / 共13页
数据结构 串与模式匹配Word文档格式.docx_第3页
第3页 / 共13页
数据结构 串与模式匹配Word文档格式.docx_第4页
第4页 / 共13页
数据结构 串与模式匹配Word文档格式.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

数据结构 串与模式匹配Word文档格式.docx

《数据结构 串与模式匹配Word文档格式.docx》由会员分享,可在线阅读,更多相关《数据结构 串与模式匹配Word文档格式.docx(13页珍藏版)》请在冰豆网上搜索。

数据结构 串与模式匹配Word文档格式.docx

2、串的模式匹配

串的模式匹配是数据结构中字符串的一种基本运算,给定一个子串,要求在某个字符串中找出与该子串相同的所有子串,这就是模式匹配。

假设P是给定的子串,T是待查找的字符串,要求从T中找出与P相同的所有子串,这个问题成为模式匹配问题。

P称为模式,T称为目标。

如果T中存在一个或多个模式为P的子串,就给出该子串在T中的位置,称为匹配成功;

否则匹配失败

【实1

验内容和要求】/

1、按照要求完成程序exp4_1.c,实现串的相关操作。

调试并运行如下测试数据给出运行结果:

•求“Thisisaboy”的串长;

•比较”abc3”和“abcde“;

表示空格

•比较”english”和“student“;

•比较”abc”和“abc“;

•截取串”white”,起始2,长度2;

•截取串”white”,起始1,长度7;

•截取串”white”,起始6,长度2;

•连接串”asddffgh”和”12344”;

实验代码:

#include<

stdio.h>

string.h>

#defineMAXSIZE100

#defineERROR0

#defineOK1

/*串的定长顺序存储表示*/

typedefstruct

{

chardata[MAXSIZE];

intlength;

}SqString;

intstrInit(SqString*s);

/*初始化串*/

intstrCreate(SqString*s);

/*生成一个串*/

intstrLength(SqString*s);

/*求串的长度*/

intstrCompare(SqString*s1,SqString*s2);

/*两个串的比较*/

intsubString(SqString*sub,SqString*s,intpos,intlen);

/*求子串*/

intstrConcat(SqString*t,SqString*s1,SqString*s2);

/*两个串的连接*/

/*初始化串*/

intstrInit(SqString*s)

s->

length=0;

data[0]='

\0'

;

returnOK;

}/*strInit*/

/*生成一个串*/

intstrCreate(SqString*s)

printf("

inputstring:

"

);

gets(s->

data);

length=strlen(s->

}/*strCreate*/

/*

(1)---求串的长度*/

intstrLength(SqString*s)

returns->

length;

}/*strLength*/

/*

(2)---两个串的比较,S1>

S2返回>

0,s1<

s2返回<

0,s1==s2返回0*/

intstrCompare(SqString*s1,SqString*s2)

inti;

for(i=0;

i<

s1->

length&

&

s2->

i++)

if(s1->

data[i]!

=s2->

data[i])

returns1->

data[i]-s2->

data[i];

length-s2->

}/*strCompare*/

/*(3)---求子串,sub为返回的子串,pos为子串的起始位置,len为子串的长度*/

intsubString(SqString*sub,SqString*s,intpos,intlen)

if(pos<

1||pos>

s->

length||len<

0||len>

length-pos+1)

returnERROR;

sub->

length=0;

len;

i++){

data[i]=s->

data[i+pos-1];

length++;

}

data[i]='

}/*subString*/

/*(4)---两个串连接,s2连接在s1后,连接后的结果串放在t中*/

intstrConcat(SqString*t,SqString*s1,SqString*s2)

inti=0,j=0;

while(i<

length){

t->

data[i]=s1->

i++;

while(j<

length)

data[i++]=s2->

data[j++];

length=s1->

length+s2->

}/*strConcat*/

intmain()

system("

color1f"

intn,k,pos,len;

SqStrings,t,x;

do

{

\n---String---\n"

1.strLentgh\n"

2.strCompare\n"

3.subString\n"

4.strConcat\n"

0.EXIT\n"

\n---String---\n"

\ninputchoice:

scanf("

%d"

&

n);

getchar();

switch(n)

case1:

\n***showstrLength***\n"

strCreate(&

s);

strLengthis%d\n"

strLength(&

s));

break;

case2:

\n***showstrCompare***\n"

t);

k=strCompare(&

s,&

/*(5)---调用串比较函数比较s,t*/

if(k==0)

twostringequal!

\n"

elseif(k<

0)

firststring<

secondstring!

else

firststring>

case3:

\n***showsubString***\n"

inputsubstringpos,len:

%d,%d"

pos,&

len);

if(subString(&

t,&

s,pos,len))

subStringis%s\n"

t.data);

posorlenERROR!

case4:

\n***showsubConcat***\n"

if(strConcat(&

x,&

t))/*(6)---调用串连接函数连接s&

t*/

Concatstringis%s"

x.data);

ConcatERROR!

case0:

exit(0);

default:

while(n);

return0;

}

2、按照要求完成程序exp4_2.c,实现BF&

KMP串的模式匹配算法。

调试及测试数据并给出结果:

•应用BF算法求子串”JING”在主串”BEIJING”中的位置,测试起始位置分别为1和5的情况;

•应用KMP算法求子串”abaabcac”在主串”acabaabaabcacaabc”中的位置,测试起始位置分别为1,10的情况,并写出子串的next[]值;

exp4_2.c部分代码如下:

intindexBf(SqString*s,SqString*t,intpos);

/*串的模式匹配BF*/

voidgetNext(SqString*t,intnext[]);

/*KMP求next值*/

intindexKmp(SqString*s,SqString*t,intstart,intnext[]);

/*串的模式匹配KMP*/

/*

(1)---串的模式匹配BF*/

intindexBf(SqString*s,SqString*t,intpos)

inti=pos-1,j=0;

j<

t->

if(s->

data[i]==t->

data[j]){

j++;

else{

i=i-j+1;

j=0;

if(j>

=t->

returni-t->

length+1;

}/*index_bf*/

/*

(2)---KMP求next值*/

voidgetNext(SqString*t,intnext[])

inti=0,j=-1;

next[0]=-1;

while(i<

if((j==-1)||(t->

data[i]==t->

data[j])){

next[i]=j;

j=next[j];

}/*getNext*/

/*(3)---KMP模式匹配*/

intindexKmp(SqString*s,SqString*t,intstart,intnext[])

inti=start-1,j=0;

j<

if(j==-1||s->

}/*index_kmp*/

intn,i,pos,next[MAXSIZE];

SqStrings,t;

1.Index_BF\n"

2.INdex_KMP\n"

\n***showIndex_BF***\n"

s:

t:

inputstartposition:

pos);

BF:

indexis%d\n"

indexBf(&

s,&

t,pos));

\n***showIndex_KMP***\n"

getNext(&

t,next);

KMP:

next[]:

t.length;

%3d"

next[i]);

indexKmp(&

t,pos,next));

【实验小结】

通过这次实验,我在其中遇到了很多问题。

比如掌握了串的存储表示及基本操作,认识到事情的解决有很多方式。

比如求next的值就BF算法以及KMP算法。

两种算法时间复杂度一个是线性的一个是非线性的,运行起的效率就会明显不一样。

所以认识问题要全面,就觉问题要多考虑。

并且还了解了串的应用,对这种结构有了更深一步的认识。

感谢下载!

欢迎您的下载,资料仅供参考

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1