题解 | #字符串通配符#
字符串通配符
https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036
#include <iostream> #include <string> #include <vector> using namespace std; int main() { string pat; string tar; getline(cin, pat); getline(cin, tar); vector<vector<bool>> dp(pat.size() + 1, vector<bool>(tar.size() + 1, false)); dp[0][0] = true; for (int i = 1; i <= pat.size(); ++i) { dp[i][0] = dp[i - 1][0] && pat[i - 1] == '*'; for (int j = 1; j <= tar.size(); ++j) { if (pat[i - 1] == '*') { dp[i][j] = dp[i][j - 1] || dp[i - 1][j]; } else if (pat[i - 1] == '?') { dp[i][j] = dp[i - 1][j - 1] && (isalpha(tar[j - 1]) || isdigit(tar[j - 1])); } else if (isalpha(pat[i - 1])) { dp[i][j] = dp[i - 1][j - 1] && isalpha(tar[j - 1]) && tolower(tar[j - 1] == tolower(pat[i - 1])); } else { dp[i][j] = dp[i - 1][j - 1] && tar[j - 1] == pat[i - 1]; } } } cout << boolalpha << dp[pat.size()][tar.size()] << endl; } // 64 位输出请用 printf("%lld")