通配符匹配

題目描述

問題描述:在計算機中,通配符一種特殊語法,普遍應用於文件搜索、數據庫、正則表達式等領域。現要求各位實現字符串通配符的算法。
要求:
實現以下2個通配符:
*:匹配0個或以上的字符(字符由英文字母和數字0-9組成,不區分大小寫。下同)
?:匹配1個字符


輸入:
通配符表達式;
一組字符串。java


輸出:
返回匹配的結果,正確輸出true,錯誤輸出false正則表達式

輸入描述:

先輸入一個帶有通配符的字符串,再輸入一個須要匹配的字符串算法

輸出描述:

返回匹配的結果,正確輸出true,錯誤輸出false數據庫

示例1

輸入

te?t*.*
txt12.xls

輸出

false

 

AC代碼(dp):spa

 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:code

 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:blog

 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 }
相關文章
相關標籤/搜索