f[i][j]
表示 a[0,...,i-1] 和 b[0,...,j-1] 能否匹配。
import java.util.*;
public class Main {
static boolean isMatch(char[] a, char[] b) {
int n = a.length, m = b.length;
boolean[][] f = new boolean[n+1][m+1];
f[0][0] = true;
for(int i=1;i <= n; i++)
f[i][0] = f[i-1][0] && a[i-1] == '*';//前面能匹配,当前位置为*,才能匹配上
for(int j=1; j <= m; j++) f[0][j] = false;
for(int i=1; i <= n; i++) {
for(int j=1; j <= m; j++) {
if(a[i-1] == '*') {
// * 匹配 0 个字符 或者 1一个以上字符
f[i][j] = f[i-1][j] || f[i][j-1];
} else {
// 前面能匹配上,当前位置a[i-1] 和 b[j-1] 能匹配上
f[i][j] = f[i-1][j-1] && (a[i-1] == '?' || a[i-1] == b[j-1]);
}
}
}
return f[n][m];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
char[] a = sc.next().toCharArray();
char[] b = sc.next().toCharArray();
System.out.println(isMatch(a, b));
}
}
}