# # # # # # # #
using namespace std ;
//关于能否匹配可用递归的方式实现
//匹配上的情况
//1.下一位是*,分三种情况:
//1.1 matchCore(str+1,pattern) 模式串匹配成功,并尝试匹配下一字符
//1.3 matchCore(str,pattern+2) 模式串未匹配 //2.下一位不是*,则pattern对应为应该与str相等或者pattern的对应为为. //matchCore(str+1,pattern + 1) //3.对应为不匹配,返回false
class Solution {
public :
bool match(char* str,char* pattern)
{
if (str == NULL || pattern == )
return false ;
matchCore(str,pattern);
}
matchCore( (*str == '\0' &&*pattern == ) true ; //迭代终止条件
(*str != ; (*(pattern + 1 ) == '*' )
{
(*str == *pattern||(*pattern== '.' &&*str!= ))
matchCore(str + ,pattern) || matchCore(str,pattern + 2 );
else );
}
(*str == *pattern || (*pattern== matchCore(str+ );
;
}
};
int main()
{
Solution so;
char * str = "aaa" ;
//char* pattern1 = "a*"; // cout << "str的长度是:" << strlen(str) << endl; * pattern1 = "ab*a*c*a" * pattern2 = "a.a" * pattern3 = "ab*a" * pattern4 = "aa.a" * pattern5 = "bbbba" * pattern6 = ".*a*a" /*char* str = "aaa";
char* pattern1 = "bbbba";*/ //char* pattern1 = ""; //
cout << "pattern1 匹配的结果是: " << so.match(str,pattern1) << endl ;
"pattern2 匹配的结果是: " "pattern3 匹配的结果是: " "pattern4 匹配的结果是: " "pattern5 匹配的结果是: " << so.match(pattern5,pattern6) << 0 ;
}
转自 https://www.cnblogs.com/Czc963239044/p/6973831.html