题解 | #字符串通配符#

字符串通配符

https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036

import java.util.*;
public class Main{
    public static void main(String []args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            String key = in.nextLine().toLowerCase(Locale.ROOT);
            String line = in.nextLine().toLowerCase(Locale.ROOT);
            boolean[] dp = new boolean[line.length() + 1];
            dp[0] = true;
            for (char k : key.toCharArray()) {
                boolean pre = dp[0];
                dp[0] = dp[0] && '*' == k;
                for (int i = 1; i < dp.length; i++) {
                    char c = line.charAt(i - 1);
                    boolean temp = dp[i];
                    if (!Character.isLetter(c) && !Character.isDigit(c)) {
                        if (k != '*') {
                            dp[i] = c == k && pre;
                        }
                    }else if (k == '?') {
                        dp[i] = pre;
                    }else if (k == '*') {
                        dp[i] |= dp[i - 1];
                    }else {
                        dp[i] = c == k && pre;
                    }
                    pre = temp;
                }
            }
            System.out.println(dp[dp.length - 1]);
        }
        in.close();
    }
}

动态规划,需要注意的细节有点多

全部评论

相关推荐

11-28 17:48
中山大学 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务