【HDOJ】2585 Hotel

本文介绍了一个简单的字符串匹配算法实现,该算法使用C语言并通过递归方式判断源字符串是否能通过目标字符串中的通配符进行匹配。文章包含完整的源代码,并通过读取输入文件的方式实现了多次测试案例的验证。

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

字符串水题。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 
 5 #define MAXN 55
 6 char src[MAXN];
 7 char des[MAXN];
 8 
 9 bool check(char *s, char *d) {
10     if (*s=='\0' && *d=='\0')
11         return true;
12     if (*s=='*' && *(s+1)=='\0')
13         return true;
14     if (*s == *d)
15         return check(s+1, d+1);
16     else if (*s=='?' && *d)
17         return check(s+1, d+1);
18     else if (*s == '*') {
19         while (*d) {
20             if (check(s+1, d))    // match more than 1 char
21                 return true;
22             ++d;
23         }
24     }
25     
26     return false;
27 }
28 
29 int main() {
30     int n;
31     int ans;
32     
33 #ifndef ONLINE_JUDGE
34     freopen("data.in", "r", stdin);
35 #endif
36 
37     while (scanf("%s", src) != EOF) {
38         scanf("%d", &n);
39         ans = 0;
40         while (n--) {
41             scanf("%s", des);
42             if (check(src, des))
43                 ++ans;
44         }
45         printf("%d\n", ans);
46     }
47     
48     return 0;
49 }

 

转载于:https://www.cnblogs.com/bombe1013/p/4179519.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值