问题描述:在计算机中,通配符一种特殊语法,广泛应用于文件搜索、数据库、正则表达式等领域。现要求各位实现字符串通配符的算法。 要求: 实现如下2个通配符: *:匹配0个或以上的字符(字符由英文字母和数字0-9组成,不区分大小写。下同)
?:匹配1个字符
【代码】
#include<iostream> #include<string> using namespace std; bool Match(char *s1, char *s2, int p, int q) { if(p<0 && q<0) return true; if(p<0 || q<0) return false; if(s1[p] == '*') return Match(s1, s2, p-1, q) || Match(s1, s2, p, q-1); if(s1[p] == s2[q] || s1[p] == '?') return Match(s1, s2, p-1, q-1); return false; } int main() { char instr[30]; char matchstr[30]; bool result; while(cin>>instr>>matchstr) { if(instr==null || matchstr==null) { cout<<"false"<<endl; return 0; } result = Match(instr, match, strlen(instr)-1, strlen(matchstr)-1); if(result) cout<<"true"<<endl; else cout<<"false"<<endl; } return 0; }