通配符匹配(C语言实现)---(1)单个字符串包含通配符

通配符匹配(C语言实现)

 

实现

  • 方法(1):递归实现

bool isMatch(const char *s, const char *p)
{
    assert(s != NULL && p != NULL);
    if(*p == '*')
    {
        while(*p=='*'){p++;}//跳出连续的*
        if(*p == '\0'){return true;}
        while(*s != '\0' && !isMatch(s,p)){s++;}
        if(*s=='\0'){return false;}
		else{return true;}
    }
    else if(*p == '\0' || *s == '\0')
    {
        return (*p==*s);
    }
    else if(*p==*s ||*p=='?')
    {
        return isMatch(++s,++p);
    }
    else {return false;}
    
    
}
  • 方法(2):迭代实现

 bool isMatch(const char *s, const char *p)
{
     assert(s != NULL && p != NULL);
     bool star=false;
     const char *str=s;
     const char *ptr=p;
    
	 while(*str!='\0')
	 {
		 switch(*ptr)
        {
            case '?':   break;
            case '*':
                star=true;
                s=str;
                p=ptr;
                while(*p=='*')
                {
                    p++;
                }
                if(*p=='\0'){return true;}
                str=s-1;
                ptr=p-1;
                break;
            default:
                if(*str!=*ptr)
                {
                    if(!star)
                    {
                        return false;
                    }
                    s++;
                    str=s-1;
                    ptr=p-1;
                }
        }
		 str++;
		 ptr++;
	 }
    while(*ptr == '*'){ptr++;}
    return (*ptr == '\0');
}
  • 方法(3):动态规划

  bool isMatch(const char *s, const char *p)
{
    assert(s != NULL && p != NULL);
    int row=strlen(p);
    int col=strlen(s);
    int **arr=(int **)calloc(row+1,sizeof(int *));
    for(int i=0;i<=row;i++)
    {
        arr[i]=(int *)calloc(col+1,sizeof(int));
    }
    arr[0][0]=1;
    
    for(int i=1;i<=row;i++)
    {
        if(p[i-1]=='*')
        {
            arr[i][0]=arr[i-1][0];
        }
    }
    
    for(int i=1;i<=row;i++)
    {
        for(int j=1;j<=col;j++)
        {
            if(p[i-1]=='*')
            {
                arr[i][j]=arr[i-1][j] || arr[i][j-1] || arr[i-1][j-1];
            }
            else
            {
                arr[i][j]=arr[i-1][j-1] && (p[i-1]==s[j-1] || p[i-1]=='?');
            }
        }
        
    }
    // for(int i=0;i<=row;i++)
    // {
		
	
        // for(int j=0;j<=col;j++)
        // {
			
            // printf("%d    ",arr[i][j]);
        // }
        // printf("\n");
    // }
	return arr[row][col];
     
}

 

借鉴:以下几篇博客的分析

https://blog.csdn.net/a83610312/article/details/9750655

https://blog.csdn.net/glDemo/article/details/47678159

https://blog.csdn.net/Scarlett_Guan/article/details/80153099

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值