1 #include"iostream"
 2 using namespace std;
 3 
 4 bool MatchCore(char*str,char* pattern);
 5 
 6 bool Match(char* str,char* pattern)
 7 {
 8     if(str==nullptr||pattern==nullptr)
 9         return false;
10     return MatchCore(str,pattern);
11 }
12 
13 bool MatchCore(char*str,char* pattern)
14 {
15     if(*str=='\0'&&*pattern=='\0')
16         return true;
17     if(*(pattern+1)=='*')
18     {
19         if(*str==*pattern||(*pattern=='.'&&*str!='\0'))
20             return MatchCore(str,pattern+2)||MatchCore(str+1,pattern+2) || MatchCore(str+1,pattern);
21         else
22             return MatchCore(str,pattern+2);
23     }
24     if(*str==*pattern||(*pattern=='.'&&*str!='\0'))
25         return MatchCore(str+1,pattern+1);
26     return false;
27 }
28 
29 void Test(char* testName,char* str,char* pattern,bool expect)
30 {
31     cout<<testName<<": ";
32     if(Match(str,pattern)==expect)
33         cout<<"passed."<<endl;
34     else
35         cout<<"failed."<<endl;
36 }
37 
38 int main()
39 {
40     Test("test1","aabcaa","a*b*.a*",true);
41     Test("test2","","",true);
42     Test("test3","aaa","a*a.",true);
43     Test("test4",nullptr,nullptr,false);
44     Test("test5","abccdde","b*ab*c*d*e.",false);
45 
46     return 0;
47 }
View Code

相关文章:

  • 2021-08-16
  • 2021-11-25
  • 2022-03-05
  • 2022-12-23
  • 2021-04-27
  • 2022-02-14
猜你喜欢
  • 2021-09-08
  • 2021-09-21
  • 2021-12-29
  • 2021-09-27
  • 2022-12-23
  • 2021-12-24
  • 2021-07-19
相关资源
相似解决方案