LeetCode: Wildcard Matching

本文介绍了一种通配符匹配算法,支持 '?' 和 '*' 的匹配,其中 '?' 可以匹配任意单个字符,而 '*' 可以匹配任意序列的字符(包括空序列)。通过两种实现方式——递归解法与非递归解法,详细探讨了如何进行完整的字符串匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Implement wildcard pattern matching with support for '?' and '*'.

'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).

The matching should cover the entire input string (not partial).

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false


递归解法,会TLE。

class Solution {
public:
    bool isMatch(const char *s, const char *p) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (*s == '\0')
        {
            if (*p == '\0')
                return true;
            else if (*p == '*')
            {
                do
                {
                    ++p;
                }while(*p == '*');
                return !(*p);
            }
            else
                return false;
        }
        else
        {
            if (*p == '\0')
                return false;
            else if('?' == *p || *p == *s)
            {
                return isMatch(s+1, p+1);
            }
            else if('*' == *p)
                return isMatch(s, p+1) || isMatch(s+1, p);
            else
                return false;
        }
    }
};

非递归:80ms

class Solution {
public:
    bool isMatch(const char *s, const char *p) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        const char *str, *pat;
        bool star = false;
        
        for (str = s, pat = p; *str != '\0'; ++str, ++pat)
        {
            switch(*pat)
            {
                // 遇到'?',那么不管*str是任何字母都能匹配
                case '?':
                    break;
                case '*':
                    star = true;
                    // 暂时忽略‘*’
                    s = str, p = pat;
                    do
                    {
                        ++p;
                    }while(*p == '*');
                     // 如果'*'之后,pat是空的,直接返回true
                    if (!*p)
                        return true;
                    // 重新开始匹配
                    str = s - 1;
                    pat = p - 1;
                    break;
                default:
                    if (*str != *pat)
                    {
                        // 如果前面没有'*',则匹配不成功
                        if (!star)
                            return false;
                        // 从s的下一位和'*'之后的p重新开始匹配
                        ++s;
                        str = s - 1;
                        pat = p - 1;
                    }
                    break;
            }
        }
        
        while (*pat == '*')
            ++pat;
        return (!*pat);
    }
};

内容概要:本文提出了一种融合多尺度Wavelet模型的跨文化英语交际智能模型系统(FL-DP-Wavelet),旨在通过多模态数据融合、多尺度特征提取与跨文化适应性建模,提升智能系统的文化敏感性和语境理解能力。该模型通过结合小波变换与深度学习优化语言信号的时频特征提取,基于跨文化敏感性发展模型(DMIS)构建文化适应性评估模块,并设计多模态数据融合框架,增强跨文化场景下的语义解析鲁棒性。实验结果显示,系统在跨文化语境下的语义理解准确率提升12.7%,文化适应性评分优于基线模型15.3%。 适合人群:从事跨文化交流、国际商务、外语教育的研究人员和技术开发者,特别是对智能系统在跨文化场景中的应用感兴趣的学者和工程师。 使用场景及目标:①跨文化商务谈判、教育合作和公共外交等场景中,需要提升智能系统的文化敏感性和语境理解能力;②帮助系统实现实时文化适应,减少因文化差异引起的语义误判和非语言行为冲突;③通过多模态数据融合,增强智能系统在复杂跨文化环境中的语义解析能力。 其他说明:该研究不仅提出了新的理论框架和技术路径,还在实际应用中验证了其有效性和优越性。未来将聚焦于小波-Transformer耦合、联邦学习隐私保护和在线学习算法,进一步推动系统向自主文化融合演进。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值