题解 | #字符串通配符#
字符串通配符
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); while (in.hasNextLine()) { String p = in.nextLine(); String b = in.nextLine(); System.out.println(f(p, b)); } } static int n1, n2; public static boolean f(String p_, String s_) { String p = p_.toLowerCase(); String s = s_.toLowerCase(); n1 = p.length(); n2 = s.length(); int[][] memo = new int[n1+1][n2+1]; boolean res = process(p, s, 0, 0, memo); return res; } public static boolean process(String p, String s, int l1, int l2, int[][] memo) { if(l1 == n1 && l2 == n2) { return true; } if(l1 == n1 && l2 < n2 || (l1 < n1 && l2 == n2)) { return false; } boolean res = false; if(memo[l1][l2] != 0) return memo[l1][l2] == 2; if(p.charAt(l1) != s.charAt(l2)) { char pc = p.charAt(l1), sc = s.charAt(l2); //1. 正常字符 if(pc != '?' && pc != '*') { } else if (pc == '?') { if(!Character.isDigit(sc) && !Character.isLetter(sc)) { } else { res = process(p, s, l1+1, l2+1, memo) || res; } } else { if(!Character.isDigit(sc) && !Character.isLetter(sc)) { } else { res = process(p, s, l1+1, l2+1, memo) || res; res = process(p, s, l1, l2+1, memo) || res; res = process(p, s, l1+1, l2, memo) || res; } } } else { res = process(p, s, l1+1, l2+1, memo) || res; } memo[l1][l2] = res ? 2 : 1; return res; } }