题解 | #字符串通配符#

字符串通配符

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;
    }
}

全部评论

相关推荐

迷茫的大四🐶:自信一点,我认为你可以拿到50k,低于50k完全配不上你的能力,兄弟,不要被他们骗了,你可以的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务