题解 | #字符串通配符#
字符串通配符
https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036
#include <iostream> using namespace std; bool match(const char* p, const char* s) { if (*p == '\0' && *s == '\0') return true; if (*p == '\0' || *s == '\0') return false; if (*p == '?') { if (!isdigit(*s) && !isalpha(*s)) return false; return match(p + 1, s + 1); } else if (*p == '*') { while (*p == '*') p++; p--; if (!isdigit(*s) && !isalpha(*s)) return false; // *匹配一个,匹配0个,匹配多个 return match(p + 1, s + 1) || match(p + 1, s) || match(p, s + 1); } else if (tolower(*p) == tolower(*s)) { return match(p + 1, s + 1); } return false; } int main() { string s1, s2; while (cin >> s1 >> s2) { bool flag = match(s1.c_str(), s2.c_str()); if (flag) cout << "true" << endl; else cout << "false" << endl; } return 0; }