通配符匹配

题目描述

问题描述:在计算机中,通配符一种特殊语法,广泛应用于文件搜索、数据库、正则表达式等领域。现要求各位实现字符串通配符的算法。
要求:
实现如下2个通配符:
*:匹配0个或以上的字符(字符由英文字母和数字0-9组成,不区分大小写。下同)
?:匹配1个字符


输入:
通配符表达式;
一组字符串。


输出:
返回匹配的结果,正确输出true,错误输出false

输入描述:

先输入一个带有通配符的字符串,再输入一个需要匹配的字符串

输出描述:

返回匹配的结果,正确输出true,错误输出false

示例1

输入

te?t*.*
txt12.xls

输出

false

 

AC代码(dp):

 1 import java.util.Objects;
 2 import java.util.Scanner;
 3 
 4 /**
 5  * 1. if '*', dp[i][j] = dp[i][j-1] || dp[i-1][j]
 6  * 2. else dp[i][j] = dp[i-1][j-1] && (a[i]==b[j] || a[i] == '?')
 7  */
 8 public class Main {
 9 
10     public static Boolean matching(String regularStr, String matchingStr) {
11         int regularLen = regularStr.length();
12         int matchingLen = matchingStr.length();
13         Boolean dp[][] = new Boolean[regularLen + 1][matchingLen + 1];
14         dp[0][0] = true;
15         for (int i = 0; i < regularLen; i++) {
16             dp[i + 1][0] = false;
17         }
18         for (int i = 0; i < matchingLen; i++) {
19             dp[0][i + 1] = false;
20         }
21         for (int i = 0; i < regularLen; i++) {
22             for (int j = 0; j < matchingLen; j++) {
23                 if (Objects.equals(regularStr.charAt(i), '*')) {
24                     dp[i + 1][j + 1] = dp[i + 1][j] || dp[i][j + 1];
25                 } else {
26                     dp[i + 1][j + 1] = dp[i][j] &&
27                             (Objects.equals(regularStr.charAt(i), matchingStr.charAt(j)) ||
28                                     (Objects.equals(regularStr.charAt(i), '?')));
29                 }
30             }
31         }
32         return dp[regularLen][matchingLen];
33     }
34 
35     public static void main(String[] args) {
36         Scanner scanner = new Scanner(System.in);
37         while (scanner.hasNextLine()) {
38             String regularStr = scanner.nextLine();
39             String matchingStr = scanner.nextLine();
40             System.out.println(matching(regularStr, matchingStr));
41         }
42     }
43 }

 

dfs:

 1 import java.util.Objects;
 2 import java.util.Scanner;
 3 
 4 public class Main {
 5 
 6     public static Boolean matching(String regularStr, String matchingStr, int i, int j) {
 7         if (i >= regularStr.length() && j >= matchingStr.length()) {
 8             return true;
 9         }
10         if (i >= regularStr.length() || j >= matchingStr.length()) {
11             return false;
12         }
13         if (Objects.equals(regularStr.charAt(i), '*')) {
14             return matching(regularStr, matchingStr, i + 1, j) ||
15                     matching(regularStr, matchingStr, i, j + 1) ||
16                     matching(regularStr, matchingStr, i + 1, j + 1);
17         } else if (Objects.equals(regularStr.charAt(i), '?')) {
18             return matching(regularStr, matchingStr, i + 1, j + 1);
19         } else if (Objects.equals(regularStr.charAt(i), matchingStr.charAt(j))) {
20             return matching(regularStr, matchingStr, i + 1, j + 1);
21         }
22         return false;
23     }
24 
25     public static void main(String[] args) {
26         Scanner scanner = new Scanner(System.in);
27         while (scanner.hasNextLine()) {
28             String regularStr = scanner.nextLine();
29             String matchingStr = scanner.nextLine();
30             System.out.println(matching(regularStr, matchingStr, 0, 0));
31         }
32     }
33 }

 

java match:

 1 import java.util.Scanner;
 2 public class Main{
 3     public static void main(String[] args) {
 4         Scanner sc = new Scanner(System.in);
 5         while(sc.hasNext()){
 6             String zhengze = sc.next();
 7             String s = sc.next();
 8             zhengze = zhengze.replaceAll("\\?","[\\\\w]{1}");
 9             zhengze = zhengze.replaceAll("\\*","[\\\\w]*");
10             System.out.println(s.matches(zhengze));
11         }
12         sc.close();
13     }
14 }

 

 

posted @ 2019-09-19 15:28  UniqueColor  阅读(1216)  评论(0编辑  收藏  举报