1 package match; 2 3 4 //遇到*考虑三种情况 匹配0个字符,匹配1个字符匹配多个字符 5 //其对应的情况分别是dp[i][j]=dp[i-1][j]||dp[i-1][j-1]||dp[i][j-1] 6 //其他dp[i][j]=dp[i-1][j-1]&&两个字符相等 7 8 9 10 import java.util.Scanner; 11 public class Main { 12 public static void main(String []args){ 13 Scanner sc=new Scanner(System.in); 14 while(sc.hasNext()){ 15 String p=sc.next(); //带*字符串 16 String q=sc.next(); //要匹配的字符串 17 boolean dp[][]=new boolean[p.length()+1][q.length()+1]; 18 dp[0][0]=true; 19 for(int i=1;i<p.length()+1;i++){ 20 for(int j=1;j<q.length()+1;j++){ 21 if(p.charAt(i-1)=='*'){ 22 dp[i][j]=dp[i-1][j]||dp[i-1][j-1]||dp[i][j-1]; 23 } 24 else{ 25 dp[i][j]=dp[i-1][j-1]&&(p.charAt(i-1)==q.charAt(j-1)); 26 } 27 } 28 } 29 System.out.println(dp[p.length()][q.length()]); 30 } 31 } 32 }