看了题解的动态规划,感觉*那一块不是很理解,*明明可以匹配多个字符,因此只从前一个字符来转移不太妥当,*那一块匹配写了备注,希望有人看懂
#include <cctype>
#include <iostream>
using namespace std;
const int maxn = 1e3;
int match[maxn][maxn];
int main() {
string str1, str2;
cin >> str1 >> str2;
int len1 = str1.size();
int len2 = str2.size();
str1 = " " + str1;
str2 = " " + str2;
match[0][0] = 1;
// * 可以匹配0个字符
for(int i = 1; i <= len1; i++){
if(str1[i] == '*') match[i][0] = match[i-1][0];
}
for(int i = 1; i <= len1; i++){
for(int j = 1; j <= len2; j++){
if(toupper(str1[i]) == toupper(str2[j]) || (str1[i] == '?' && isalnum(str2[j]))){
match[i][j] = match[i-1][j-1];
}
else if(str1[i] == '*'){
match[i][j] = match[i-1][j]; // *匹配空字符
for(int k = j; k >= 1; k--){ //*匹配多个字符
if(isalnum(str2[k])) match[i][j] = match[i-1][k-1] || match[i][j]; //如果是字母和数字,说明这后面的一排都可以用*匹配
else break; // 一旦出现不是字母和数字的情况,那么此时不能用*匹配
}
}
}
}
if(match[len1][len2]) cout << "true";
else cout << "false";
}
// 64 位输出请用 printf("%lld")



京公网安备 11010502036488号