sunday算法(有bug)不能count

//实现了 count,但是针对最后的 关键字不能计算在内
#include<stdio.h>
#include<string.h>

int ISFindTheCh(char * const target,char ch);

int IsMatch(char *const source,char * target);

int main()
{
	char string[] = "hellohello world!,worryhellow";
	char str[] ="wor";
    static int numWord = 0;
    static int count = 0;
    int Source_Lenth =(int) strlen(string);
	int Target_Lenth = (int) strlen(str);
    
    //while( count + Target_Lenth < Source_Lenth)
    while( count < Source_Lenth)
    {
        char *strterm;
        if(count == 0)
            strterm = string;
        else
            strterm = string +count+numWord*Target_Lenth;
        
        int offset = IsMatch(strterm,str);
        if (offset < 0)
        {
            printf("No Find '%s' again. \n",str);
            printf("the find num of the '%s' is %d.\n",str,numWord);
            
            return 0;
        }
        numWord +=1;
        count += offset;
        printf("Find '%s' in offset: %d \n",str,count);
    }
    
	
	
	return 1 ;
}

int ISFindTheCh(char * const target,char ch)
{
	char * p = strrchr(target,ch);//从后往前匹配的第一个字符
    if(p)
		return (int)(p - target);
	else
		return -1;
}

int IsMatch(char *const source,char * target)
{
	int Source_Lenth =(int) strlen(source);
	int Target_Lenth = (int)strlen(target);
	
	char *Sp = source;//源文件指针
	char *St = target;//目标文件指针
	char *SumP = source + Target_Lenth;
    //    if ((Source_Lenth - Target_Lenth) <0)
    //        return -1;
	
	int offset = -1;
	int count = 0;
	while(Source_Lenth  > Target_Lenth)
	{
		
		if(*Sp == *St)
		{
			Sp++;
			St++;
			Source_Lenth--;
			count++;
			if(count == Target_Lenth)
            {
                // printf("---Find '%s' in offset: %ld \n",target,Sp-source-count);
				return (int)(Sp-source-count);
                
                
            }
		}
		else
		{
		   	offset = ISFindTheCh(target,SumP[0]);
			if(offset == -1)
			{
				Sp = SumP +1 ;
                SumP = Sp + Target_Lenth;
			}
			else
			{
				Sp = SumP - offset;
				Source_Lenth += offset;
			}
			
			St = target;
			count = 0;
			Source_Lenth -= Target_Lenth ;
			
		}
        
	}
    // printf("No Find '%s' again. \n",target);
    return -1;
	
}


//不能计数count、
#include<stdio.h>
#include<string.h>


int ISFindTheCh(char * const target,char ch);


int IsMatch(char *const source,char * target);


int mian()
{
char string[] = "hello world!";
char str[] ="str";
int is;
if((is = IsMatch(string,str)))
printf("Find '%s' in offset: %d \n",str,is);

return 1 ;
}


int ISFindTheCh(char * const target,char ch)
{
char * p = strrchr(target,ch);//从后往前匹配的第一个字符
    if(p)
return p - target;
else
return -1;
}


int IsMatch(char *const source,char * target)
{
int Source_Lenth = strlen(source);
int Target_Lenth = strlen(target);

char *Sp = source;//源文件指针
char *St = target;//目标文件指针
char *SumP = source + Target_Lenth;

int offset = -1;
int count = 0;
while(Source_Lenth > 0)
{

if(*Sp == *St)
{
Sp++;
St++;
Source_Lenth--;
count++;
if(count == Target_Lenth)
return Sp-source-count;
}
else
{
  offset = ISFindTheCh(source,SumP[0]);
if(offset == -1)
{
Sp = SumP +1 ;
                SumP = Sp + Target_Lenth;
}
else
{
Sp = SumP - offset;
Source_Lenth += offset;
}

St = target;
count = 0;
Source_Lenth -= Target_Lenth ;

}

}
   return -1; 

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值