44 - Wildcard Matching(C)

本文介绍了一种实现通配符匹配的算法,支持 '?' 和 '*' 两种通配符,其中 '?' 匹配任意单个字符,'*' 匹配任意序列的字符。文章详细解析了算法思路,并给出 C++ 实现代码。

摘要生成于 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

思路分析:


For each element in s
If *s==*p or *p == ? which means this is a match, then goes to next element s++ p++.
If p=='*', this is also a match, but one or many chars may be available, so let us save this *'s position and the matched s position.
If not match, then we check if there is a * previously showed up,
       if there is no *,  return false;
       if there is an *,  we set current p to the next element of *, and set current s to the next saved s position.

e.g.

abed
?b*d**

a=?, go on, b=b, go on,
e=*, save * position star=3, save s position ss = 3, p++
e!=d,  check if there was a *, yes, ss++, s=ss; p=star+1
d=d, go on, meet the end.
check the rest element in p, if all are *, true, else false;

Note that in char array, the last is NOT NULL, to check the end, use  "*p"  or "*p=='\0'".


aabbccssssssd

?a?b?c*d




代码如下:

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* star=NULL;
        const char* ss=s; 
        while (*s){
            if ((*p=='?')||(*p==*s)){s++;p++;continue;}
            if (*p=='*'){star=p++; ss=s;continue;}
            if (star){ p = star+1; s=++ss;continue;}
            return false;
        }
        while (*p=='*'){p++;}
        return !*p;
    }
};


<think>好的,我现在需要解决用户在Spring MVC配置文件中遇到“mvc:annotation-driven”元素声明找不到的问题。首先,我应该回顾用户提供的引用内容,看看是否有相关线索。引用[1]和[2]主要涉及Spring MVC中的@InitBinder方法和控制器方法的使用,可能和配置命名空间或版本有关。但用户的问题是关于XML配置中的元素声明缺失,所以可能需要检查Spring的命名空间配置是否正确。 首先,用户可能没有正确声明MVC命名空间。在Spring的XML配置文件中,顶部需要正确的命名空间声明,比如xmlns:mvc="http://www.springframework.org/schema/mvc"。如果这个声明缺失或拼写错误,就会导致元素无法识别。 其次,可能需要检查schemaLocation是否正确。schemaLocation应该包含MVC的XSD文件路径,例如http://www.springframework.org/schema/mvc/spring-mvc.xsd。如果版本不匹配或者路径错误,解析器可能找不到对应的定义。 另外,用户可能使用的Spring版本较旧,不支持“mvc:annotation-driven”元素。该元素是在Spring 3.0之后引入的,如果用户的项目使用的是旧版本,比如2.5,那么确实会找不到这个声明。这时候需要升级Spring版本或者改用其他配置方式。 还有一个可能性是配置文件中的拼写错误,比如元素名称写错了,或者标签没有正确闭合,导致解析错误。需要用户仔细检查XML文件的结构和语法是否正确。 此外,构建工具如Maven或Gradle是否正确引入了spring-webmvc依赖。如果依赖缺失或版本不对,也会导致相关类无法加载,进而影响XML配置的解析。 总结下来,可能的解决步骤包括:检查命名空间声明、验证schemaLocation、确认Spring版本、检查XML语法以及确保依赖正确。需要一步步引导用户排查这些可能性,找到具体原因并解决。</think>要解决Spring MVC配置文件中出现`mvc:annotation-driven`元素声明找不到的问题,请按照以下步骤排查: ### 1. 检查命名空间声明 确保XML配置文件的根标签中**正确声明了MVC命名空间**: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" <!-- 关键声明 --> xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 必须包含 --> ``` ### 2. 验证Spring版本 `mvc:annotation-driven`需要**Spring 3.0+版本**支持: - 查看`pom.xml`/`build.gradle`中`spring-webmvc`依赖版本 - 推荐使用Spring 5.x版本: ```xml <!-- Maven示例 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.18</version> </dependency> ``` ### 3. 检查XML语法 确认配置段**没有嵌套错误**,典型正确配置示例: ```xml <mvc:annotation-driven> <mvc:message-converters> <!-- 可选扩展配置 --> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven> ``` ### 4. 验证IDE配置 在IntelliJ/Eclipse中: 1. 右键点击配置文件 → Validate 2. 检查是否有红色波浪线报错 3. 确保IDE已正确索引Spring库 ### 5. 运行时验证 若启动时出现`org.xml.sax.SAXParseException`,说明XML解析失败,需检查: ```log Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration exists for element 'mvc:annotation-driven' ``` ### 附:功能说明 `mvc:annotation-driven`会自动注册: - `@RequestMapping`处理器映射 - 支持`@RequestBody`/`@ResponseBody`的转换器(如JSON) - 数据绑定与验证器(如@Valid) - 类型转换服务[^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值