题解 | #字符串通配符#
字符串通配符
https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036
import java.util.Scanner;
public class Main {
public static void main(String[] args) { Scanner in = new Scanner(System.in); String pat = in.next().toLowerCase(); String input = in.next().toLowerCase(); int len1 = pat.length(); int len2 = input.length(); boolean dp[][] = new boolean[len2 + 1][len1 + 1]; char[] d1 = pat.toCharArray(); char[] d2 = input.toCharArray(); dp[0][0] = true;; for (int i = 1; i <= len1; i++) { if (d1[i - 1] == '*') { dp[0][i] = dp[0][i - 1]; } else dp[0][i] = false; } for (int j = 1; j <= len2; j++) { dp[j][0] = false; } for (int i = 1; i <= len2; i++) { for (int j = 1; j <= len1; j++) { if (i == 2 && j == 3) { int eee = 1; } if (d1[j - 1] == d2[i - 1]) { dp[i][j] = dp[i - 1][j - 1]; } else if (d1[j - 1] == '?' && (Character.isLetter(d2[i - 1]) || Character.isDigit(d2[i - 1]))) { dp[i][j] = dp[i - 1][j - 1]; } else if (d1[j - 1] == '*' && (Character.isLetter(d2[i - 1]) || Character.isDigit(d2[i - 1]))) { dp[i][j] = dp[i - 1][j] || dp[i][j - 1] || dp[i - 1][j - 1]; } else { dp[i][j] = false; } } } System.out.println(dp[len2][len1]); }
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String pat = in.next().toLowerCase();
String input = in.next().toLowerCase();
int len1 = pat.length();
int len2 = input.length();
boolean dp[][] = new boolean[len2 + 1][len1 + 1];
char[] d1 = pat.toCharArray();
char[] d2 = input.toCharArray();
dp[0][0] = true;;
for (int i = 1; i <= len1; i++) {
if (d1[i - 1] == '*') {
dp[0][i] = dp[0][i - 1];
} else
dp[0][i] = false;
}
for (int j = 1; j <= len2; j++) {
dp[j][0] = false;
}
for (int i = 1; i <= len2; i++) {
for (int j = 1; j <= len1; j++) {
if (i == 2 && j == 3) {
int eee = 1;
}
if (d1[j - 1] == d2[i - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else if (d1[j - 1] == '?' && (Character.isLetter(d2[i - 1]) ||
Character.isDigit(d2[i - 1]))) {
dp[i][j] = dp[i - 1][j - 1];
} else if (d1[j - 1] == '*' && (Character.isLetter(d2[i - 1]) ||
Character.isDigit(d2[i - 1]))) {
dp[i][j] = dp[i - 1][j] || dp[i][j - 1] || dp[i - 1][j - 1];
} else {
dp[i][j] = false;
}
}
}
System.out.println(dp[len2][len1]);
}
}