Sunday字符串匹配

Sunday字符串匹配的过程是从模式串的右端向左端匹配,如果匹配失败,则根据匹配串末位字符对应文本串的位置的下一个字符c判断移动距离,如果c在匹配串中,则移动距离D=匹配串中该字符距离匹配串末位最近距离+1,如果c没有出现在匹配串中,则D=匹配串的长度。

#include <iostream>
#include <cassert>


using namespace std;


int sunday(const char *text, const char *temp)
{
assert(text!=NULL && temp!=NULL);
int tLen = strlen(temp);
int textLen = strlen(text);
assert(textLen >= tLen);
int *skip = new int[256];
int i=0;
for(i=0;i<256;i++)
skip[i] = tLen;
//temp中字符的最右距离
const char *ptr = temp;
for(i=0;i<tLen;i++)
skip[temp[i]] = tLen - i;
//从匹配串的右端向左匹配
int j=tLen - 1;
while(j<textLen)
{
int k = j;
i = tLen - 1;
while(i>=0 && text[k--]==temp[i--]);
i++;
k++;
if(i==0 && text[k]==temp[i])
return k;
j = j + skip[text[j+1]];
}
return -1;
}


int main()
{
char *str1 = "tiansilab";
char *str2 = "ti";
int k = sunday(str1, str2);


cout<<k<<endl;
return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值