rambless
字符串通配符
https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
//除了*和?,如果不存在,则一定false
String reg = in.nextLine().toLowerCase();
String str = in.nextLine().toLowerCase();
match(reg, str);
}
}
private static void match(String reg, String str) {
boolean[][] dp = new boolean[reg.length()+1][str.length()+1];
//首列初始化
dp[0][0] = true;
for(int i=0; i<reg.length(); i++) {
if(reg.charAt(i)=='*') {
dp[i+1][0] = true;
} else {
break;
}
}
//从第一列开始
char r, s;
for(int i=0; i<reg.length(); i++) {
r = reg.charAt(i);
for(int j=0; j<str.length(); j++) {
s = str.charAt(j);
//如果字符相同
if(r==s) {
dp[i+1][j+1] = dp[i][j];
}
//如果是'?'
if(r=='?' && (Character.isDigit(s) || (97<=s && s<=122))) {
dp[i+1][j+1] = dp[i][j];
}
//如果是'*'
if(r=='*') {
dp[i+1][j+1] = dp[i+1][j] || dp[i][j+1] || dp[i][j];
}
}
}
System.out.println(dp[reg.length()][str.length()]);
}
}
SHEIN希音公司福利 363人发布
查看10道真题和解析