第一行输入一个长度为
、由可见字符构成的通配符字符串
。
第二行输入一个长度为
、由可见字符构成的目标字符串
。
如果可以匹配得到,输出
;否则,输出
。
z zz
false
Z* zz
true
在这个样例中,
匹配
。注意,由于不区分大小写,
可以匹配
。
?* zz
true
在这个样例中,
匹配
、
匹配
。
**Z 0QZz
true
在这个样例中,其中一种匹配方法是:
第一个
匹配
;
第二个
匹配
;
第三个
匹配
。
??** zz
true
可以匹配
个字符。
HH?* HH##1
false
可被
和
匹配的字符不包含
。
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
String regex = in.nextLine().toLowerCase();
String s = in.nextLine().toLowerCase();
System.out.println(isMatched(regex, s));
}
private static boolean isMatched(String regex, String s) {
int regexLength = regex.length();
int sLength = s.length();
boolean[][] result = new boolean[regexLength + 1][sLength + 1];
for (int j = 0; j < sLength + 1; j++) {
result[regexLength][j] = (j == sLength);
}
for (int i = regexLength - 1; i > -1; i--) {
for (int j = 0; j < sLength; j++) {
switch (regex.charAt(i)) {
case '*':
result[i][j] = result[i + 1][j]; //只有“*”才可以匹配空串
for (int k = j + 1; k < sLength + 1 ; k++) {
result[i][j] = result[i][j] || result[i + 1][k];
if (k < sLength && isCommonChar(s.charAt(k))) //“*”并不能匹配一般字符,所以不应该继续查找
break;
}
break;
case '?':
if (isCommonChar(s.charAt(j))) { //“?”必须匹配至少1个数字或字母
result[i][j]=false;
break;
}
for (int k = j + 1; k < sLength + 1 ; k++) {
result[i][j] = result[i][j] || result[i + 1][k];
if (k < sLength && isCommonChar(s.charAt(k))) //和上面类似的判断逻辑
break;
}
break;
default:
result[i][j] = regex.charAt(i) == s.charAt(j) && result[i + 1][j + 1]; //对于一般字符,不但要对应相等,还要保证在子问题上也匹配
break;
}
}
result[i][sLength] = regex.charAt(i) == '*' && result[i + 1][sLength]; //检查此处的通配串是否可以匹配空串“”
}
return result[0][0];
}
private static boolean isCommonChar(char c) {
return (c < 'a' || c > 'z') && (c < '0' || c > '9');
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
boolean flag = false;
Scanner in = new Scanner(System.in);
String s1 = in.nextLine().toLowerCase();
String s2 = in.nextLine().toLowerCase();
flag=isSame(s1,s2);
System.out.println(flag);
}
public static boolean isSame(String s1, String s2) {
int i = 0, j = 0, index = 0;
for (; i < s1.length() && j < s2.length(); j++) {
if (s1.charAt(i) == '?') { //第1种情况字符为‘?’
if (Character.isLetter(s2.charAt(j)) || Character.isDigit(s2.charAt(j))) {
i++;
} else {
break;
}
} else if (s1.charAt(i) == '*' && (i+1) < s1.length()) { //第2种情况字符为‘*’,且不在字符串末尾
i++;
while(i < s1.length()&&(s1.charAt(i) == '*'||s1.charAt(i) == '?')){
i++;
}
if(i==s1.length()){
return true;
}
while (j<s2.length()&&s1.charAt(i) != s2.charAt(j)) {
if(!Character.isLetter(s2.charAt(j)) &&!Character.isDigit(s2.charAt(j))){
if(s1.charAt(i) != s2.charAt(j)){
return false;
}
}
j++;
}
if (j == s2.length()) {
break;
}
index = j;
if(isSame(s1.substring(i, s1.length()), s2.substring(j, s2.length()))){
return true;
}
while (j<s2.length()&&!isSame(s1.substring(i, s1.length()), s2.substring(j, s2.length()))) {
j=index+1;
while (j<s2.length()&&s1.charAt(i) != s2.charAt(j)) {
j++;
}
if (j == s2.length()) {
break;
}
if(isSame(s1.substring(i, s1.length()), s2.substring(j, s2.length()))){
return true;
}
index = j;
}
} else if (s1.charAt(i) == '*' && (i + 1) == s1.length()) { //第2种情况字符为‘*’,在字符串末尾
return true;
} else { //第3种情况字符为普通字符
if (s1.charAt(i) == s2.charAt(j)) {
i++;
} else {
break;
}
}
}
if(i==s1.length()&&j==s2.length()){
return true;
}else if(j==s2.length()&&s1.substring(i,s1.length()).split("[*?]").length==0){
return true;
}else{
return false;
}
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next().toUpperCase(); // 都转大写
String p = in.next().toUpperCase(); // 都转大写
// // 去除s中连续*,防止超时
// String ss = "";
// int i = 0;
// while (i < s.length()) {
// if (s.charAt(i) != '*') {
// ss += s.charAt(i);
// i++;
// } else {
// ss += '*';
// while (i < s.length() && s.charAt(i) == '*') {
// i++;
// }
// }
// }
// // 构造正则
// String regex = "";
// for (char c : ss.toCharArray()) {
// if (c == '*') {
// regex += "[0-9A-Z]*"; // * :0个或以上的数字或字母;
// } else if (c == '?') {
// regex += "[0-9A-Z]"; // ? :1个数字或字母
// } else {
// regex += c; // 其他: 由于都转大写,匹配自身
// }
// }
String regex = s.replaceAll("[*]+", "[0-9A-Z]*").replaceAll("[?]", "[0-9A-Z]"); // 看了别人的,直接replace
System.out.print(p.matches(regex));
}
}
// 不去重,就会超时的用例
// h*h*ah**ha*h**h***hha
// hhhhhhhahhaahhahhhhaaahhahhahaaahhahahhhahhhahaaahaahs
import java.util.Scanner;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String pattern=in.nextLine().toLowerCase();
String s=in.nextLine().toLowerCase();
pattern=pattern.replaceAll("\\*+","*");
pattern=pattern.replace(".", "\\.");
pattern=pattern.replace("?", "([a-z]|[0-9])+");
pattern=pattern.replace("*", "([a-z]|[0-9])*");
//System.out.println(pattern);
System.out.println(Pattern.matches(pattern,s)); ;
}
} 有一种简单粗暴的美 import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String reg = in.nextLine().toLowerCase();
String str = in.nextLine().toLowerCase();
reg = reg.replaceAll("\\*+","[a-z0-9]*").replaceAll("\\?","[a-z0-9]");
System.out.println(str.matches(reg));
}
} // 用正则替换
public static boolean resolve(String s, String content) {
s = s.toLowerCase();
content = content.toLowerCase();
// 多个*相当于一个*
s = s.replaceAll("\\*+", "*");
// *?*或*?或?*相当于匹配至少一个字符
s = s.replaceAll("\\*\\?\\*|\\*\\?|\\?\\*", "[0-9a-z]+");
s = s.replaceAll("\\*", "[0-9a-z]*");
s = s.replaceAll("\\?", "[0-9a-z]{1}");
s = "^" + s + "$";
return content.matches(s);
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String tpstr = sc.nextLine().toLowerCase();//通配符
String needstr = sc.nextLine().toLowerCase();//需要被匹配的
System.out.println(isMatch(tpstr,needstr));
}
}
public static boolean isMatch(String s, String p) {
//只向右向下向右下
boolean[][] dp=new boolean[s.length()+1][p.length()+1];
dp[0][0]=true;
//第一行为'*'则一行都为true;
for(int i=1;i<=p.length();i++){
if(s.charAt(0)=='*'){
dp[1][i]=true;
}
}
for(int i=1;i<=s.length();i++){
for(int j=1;j<=p.length();j++){
if(s.charAt(i-1)==p.charAt(j-1)){
dp[i][j]=dp[i-1][j-1]||dp[i-1][j];
}
if(s.charAt(i-1)=='?'&&((p.charAt(j-1)>='a'&&p.charAt(j-1)<='z')||(p.charAt(j-1)>='0'&&p.charAt(j-1)<='9'))){
dp[i][j]=dp[i-1][j-1];
}
if(s.charAt(i-1)=='*'&&((p.charAt(j-1)>='a'&&p.charAt(j-1)<='z')||(p.charAt(j-1)>='0'&&p.charAt(j-1)<='9'))){
dp[i][j]=dp[i-1][j]||dp[i][j-1]||dp[i-1][j-1];
}
}
}
return dp[s.length()][p.length()];
}
} import java.util.*;
//用正则表达式打败模糊匹配!
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String matchRule = in.next().toLowerCase(Locale.ROOT);//将字符串中的大写字母转小写
String scStr = in.next().toLowerCase(Locale.ROOT);
matchRule = matchRule.replaceAll("\\*+", "[a-z0-9]*");//替换1个或多个*
matchRule = matchRule.replaceAll("\\?", "[a-z0-9]");//替换问号
System.out.print(scStr.matches(matchRule));
}
}
// 和普通的区别是, * 和 ? 只能表数字和字母(所以要加上额外判断)
// 注意: * 表 0 个时不需要是数字或者字母
import java.lang.String;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String pattern;
while ((pattern = br.readLine()) != null) {
pattern = toLowerStr(pattern.trim());
String str = toLowerStr(br.readLine().trim());
System.out.println(isMatch(str, pattern));
}
}
public static String toLowerStr(String str) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
if (Character.isUpperCase(str.charAt(i))) {
sb.append(Character.toLowerCase(str.charAt(i)));
} else {
sb.append(str.charAt(i));
}
}
return sb.toString();
}
public static boolean isMatch(String s, String p) {
int m = s.length();
int n = p.length();
boolean[][] dp = new boolean[m + 1][n + 1];
dp[0][0] = true;
for (int i = 1; i <= n; ++i) {
if (p.charAt(i - 1) == '*') {
dp[0][i] = true;
} else {
break;
}
}
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (s.charAt(i - 1) == p.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else {
boolean isLetterOrDigit = Character.isLetter(s.charAt(i - 1)) ||
Character.isDigit(s.charAt(i - 1));
if (p.charAt(j - 1) == '*') {
// 表 0 个时不需要是数字或者字母
dp[i][j] = (dp[i - 1][j] && isLetterOrDigit) || dp[i][j - 1];
} else if (p.charAt(j - 1) == '?' && isLetterOrDigit) {
dp[i][j] = dp[i - 1][j - 1];
}
}
}
}
return dp[m][n];
}
} 我这个很ok
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String a = "";
String b = "";
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
a = in.nextLine().toLowerCase(Locale.ROOT);
b = in.nextLine().toLowerCase(Locale.ROOT);
}
//多个星号换成一个星号
String regex = a.replaceAll("\\*{2,}","\\*");
// //?换成一个0-9a-z正则
regex = regex.replace("?", "[0-9a-z]{1}");
//*换成n个0-9a-z正则
regex = regex.replace("*", "[0-9a-z]{0,}");
boolean isMatch = b.matches(regex);
System.out.println(isMatch);
}
} import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String regex = in.next().toLowerCase();
String target = in.next().toLowerCase();
boolean dp[][]=new boolean[target.length() + 1][regex.length() + 1];
dp[0][0]=true;
for(int i = 1; i <= target.length(); i++){
if (regex.charAt(i - 1) == '*') {
dp[0][i] = true;
} else {
break;
}
}
for(int i = 1; i <= target.length(); i++){
for(int j = 1 ; j <= regex.length(); j++){
if (target.charAt(i - 1) == regex.charAt(j - 1)) {
dp[i][j]=dp[i-1][j-1];
} else if (Character.isDigit(target.charAt(i - 1)) || Character.isLetter(target.charAt(i - 1))) {
if (regex.charAt(j - 1) == '*') {
dp[i][j] = dp[i - 1][j - 1] || dp[i - 1][j] || dp[i][j - 1];
} else if (regex.charAt(j - 1) == '?') {
dp[i][j] = dp[i - 1][j - 1];
}
}
}
}
System.out.println(dp[target.length()][regex.length()]);
}
}
}
import java.util.*;
public class Main {
// 状态定义:f(i, j): s2 的前 i 个字符能否匹配 s1 的前 j 个字符
// 状态转移方程:
// (1) 如果 s1 的第 j 个字符是 '?',说明 s1 的第 j 个字符可以和 s2 的第 i 个字符匹配
// 看 s1 的前 j - 1 个能否与 s2 的前 i - 1 个匹配,即 f(i, j) = f(i - 1, j - 1)
// (2) 如果 s1 的第 j 个字符是非通配符,则看 s1 的第 j 个字符能否和 s2 的第 i 个字符匹配
// 如果能,就是 f(i, j) = f(i - 1, j - 1);如果不能,f(i, j) = false
// (3) 如果 s1 的第 j 个字符是 '*',则有三种可能:
// ① '*' 匹配 0 个 s2[i],即 f[i][j] = f[i][j - 1]
// ② '*' 匹配 1 个 s2[i],即 f[i][j] = f[i - 1][j - 1]
// ③ '*' 匹配多个 s2[i],即 f[i][j] = f[i - 1][j]
// 初始化:dp[0][0]代表两个空串匹配。dp[i][0]是 s2 和空串匹配,永远为 false
// dp[0][j]是 s1 和空串匹配,当 s1 只有 *,则和空串匹配为 true,出现一个非 *,则匹配为 false
public static boolean process(String s1, String s2) {
int n1 = s1.length(); // 通配符串
int n2 = s2.length(); // 匹配串
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
// 二维表中,横的是通配符串,纵的是匹配串
boolean[][] dp = new boolean[n2 + 1][n1 + 1];
// 初始化
dp[0][0] = true;
for(int i = 1; i <= n2; i++) { // c2 和空串匹配,永远为 false
dp[i][0] = false;
}
boolean flg = false;
for(int j = 1; j <= n1; j++) {
// c1 只有 *,则和空串匹配为 true,出现一个非 *,则匹配为 false
if(!flg && c1[j - 1] == '*') {
dp[0][j] = true;
} else {
flg = true;
dp[0][j] = false;
}
}
for(int i = 1; i <= n2; i++) {
for(int j = 1; j <= n1; j++) {
if(c1[j - 1] == '*') {
// 只有英文字母和数字能被通配符匹配
if((c2[i - 1] >= '0' && c2[i - 1] <= '9') || (c2[i - 1] >= 'a' && c2[i - 1] <= 'z')
|| (c2[i - 1] >= 'A' && c2[i - 1] <= 'Z')) {
dp[i][j] = dp[i - 1][j] || dp[i][j - 1] || dp[i - 1][j - 1];
} else { // 不匹配,为 false
dp[i][j] = false;
}
} else { // ? 和普通字符
if(match(c1[j - 1], c2[i - 1])) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = false;
}
}
}
}
return dp[n2][n1];
}
public static boolean match(char c1, char c2) {
if(c1 == '?') return true;
// 小写字母全部转为大写字母再比较
if(c1 >= 'a' && c1 <= 'z') {
c1 = (char)(c1 - 'a' + 'A');
}
if(c2 >= 'a' && c2 <= 'z') {
c2 = (char)(c2 - 'a' + 'A');
}
return c1 == c2;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.next();
String s2 = sc.next();
System.out.println(process(s1, s2));
}
} import java.io.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String p;
while ((p = br.readLine()) != null) {
String s = br.readLine();
System.out.println(isMatch(p, s));
}
}
public static boolean isMatch(String p, String s) {
int m = p.length() + 1;
int n = s.length() + 1;
s = s.toLowerCase();
p = p.toLowerCase();
boolean[][] dp = new boolean[m][n];
dp[0][0] = true;
if (p.charAt(0) == '*') {
for (int i = 0; i < n; i++) {
dp[1][i] = true;
}
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (p.charAt(i - 1) == s.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else if (p.charAt(i - 1) == '*' || p.charAt(i - 1) == '?') {
char c = s.charAt(j - 1);
boolean b = (c >= 48 && c <= 57) || (c >= 97 && c <= 122);
if (p.charAt(i - 1) == '*') {
dp[i][j] = dp[i - 1][j] || (dp[i][j - 1] && b);
} else if (p.charAt(i - 1) == '?' && b) {
dp[i][j] = dp[i - 1][j - 1];
}
}
}
}
// for (boolean[] t : dp) {
// System.out.println(Arrays.toString(t));
// }
return dp[m - 1][n - 1];
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String regex=sc.nextLine();
String str=sc.nextLine();
System.out.println(match(regex,str));
}
public static boolean match(String regex,String str){
int m=str.length();
int n =regex.length();
char[] strs=str.toCharArray();
char[] regexs=regex.toCharArray();
boolean[][] dp=new boolean[n+1][m+1];
dp[0][0]=true;
for (int i=0;i<m;i++){
if(Character.isLetter(strs[i])) strs[i]=Character.toLowerCase(strs[i]);
}
for(int i=1;i<=n;i++){
if(regexs[i-1]=='*')dp[i][0]=dp[i-1][0];
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!isWord(strs[j-1])){
// 如果是特殊字符,必须这个值相等才能匹配上,否则直接为false
if(regexs[i-1]==strs[j-1])dp[i][j]=dp[i-1][j-1];
continue;
};
char c=strs[j-1];
// 如果不是特殊字符,是*的情况则只要dp[i-1][j]||dp[i-1][j-1]||dp[i][j-1]=true都能匹配上
if(regexs[i-1]=='*')dp[i][j]=dp[i-1][j]||dp[i-1][j-1]||dp[i][j-1];
// 如果不是特殊字符,是?或相等的情况只要dp[i-1][j-1]=true就能匹配上
else if(regexs[i-1]=='?'||Character.toLowerCase(regexs[i-1])==c)dp[i][j]=dp[i-1][j-1];
}
}