c语言数据结构之线性表串的模式匹配(六)简单的模式匹配算法

在这里插入图片描述

 子串的定位操作通常称为串的模式匹配,它求的是子串(常称模式串)在主串中的位置。这里采用定长顺序存储结构,给出一种不依赖于其他串操作的

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct sstring{
	char ch[256];
	int len;
}sstring;
//在母串a中寻找子串 
int matEle(sstring *a,sstring *b)
{
	int i=0;//母串的下标 
	int j=0;//子串的下标
	int count=0,note=0;//用于计数 
	int len1=strlen(a->ch);//母串的长度
	int len2=strlen(b->ch);//子串的长度
	while(i<=len1)
	{
		note=i;
		while(a->ch[i]==b->ch[j])
		{			
			i++;j++;
			count++;
		}
		if(count!=len2)
		{
			j=0;
			++note;
			i=note;
			count=0;			
		}
		else
		{
			return note;
			break;
		}
				
	} 
	
}

int main()
{
	sstring *a=(sstring *)malloc(sizeof(sstring *));
	sstring *b=(sstring *)malloc(sizeof(sstring *));
	strcpy(a->ch,"aaabbbccaaabbbddd");
	strcpy(b->ch,"bbbdd");
	int res=matEle(a,b);
	printf("%d ",res);
	return 0;
}

暴力匹配算法。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值