题解 | #字符串通配符#
字符串通配符
https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036
#include <iostream> using namespace std; bool match(const string& pattern, const string& source, int i, int j) { int n1 = pattern.size(); int n2 = source.size(); if (i == n1 && j == n2) return true; if (i < n1 && j == n2 || i == n1 && j < n2) return false; if (pattern[i] == '?') { if (!isalnum(source[j])) return false; return match(pattern, source, i + 1, j + 1); } else if (pattern[i] == '*') { while (pattern[i] == '*') { i++; } i--; return match(pattern, source, i + 1, j) // 0 || (isalnum(source[j]) && match(pattern, source, i + 1, j + 1)) // 1 || (isalnum(source[j]) && match(pattern, source, i, j + 1)); } else if (tolower(pattern[i]) == tolower(source[j])) { return match(pattern, source, i + 1, j + 1); } return false; } int main() { string str1, str2; cin >> str1 >> str2; if (match(str1, str2, 0, 0)) { cout << "true" << endl; } else { cout << "false" << endl; } return 0; }