题解 | #字符串通配符#
字符串通配符
https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036
#include <iostream> #include <vector> using namespace std; // 动态规划,str包含?或* int isMatch(string str, string s){ vector<vector<int>> dp(str.size()+1, vector<int>(s.size()+1,0)); dp[0][0] = 1; for (int i = 1; i <= str.size(); ++i){ dp[i][0] = dp[i-1][0] && (str[i-1] == '*'); for (int j = 1; j <= s.size(); ++j){ if (str[i-1] == '*'){ dp[i][j] = dp[i-1][j] || dp[i][j-1]; }else if (isdigit(s[j-1])){ dp[i][j] = dp[i-1][j-1] && ((str[i-1] == '?') || (str[i-1] == s[j-1]));}else if (isalpha(s[j-1])){ dp[i][j] = dp[i-1][j-1] && (str[i-1] == '?'|| tolower(str[i-1]) == tolower(s[j-1]));}else { dp[i][j] = dp[i-1][j-1] && (str[i-1] == s[j-1]); } } } return dp[str.size()][s.size()]; } int main() { string str, s; while (cin >> str >> s) { if (isMatch(str, s)){ cout << "true" << endl; }else { cout << "false" << endl; } } return 0; }#你觉得今年春招回暖了吗##23届找工作求助阵地##14天打卡计划##零基础学习C++#