剑指offer19

#include<iostream>
using namespace std;

bool check(char* str, char* pattern,int i,int j) {
	if (str[i] == '\0'&&pattern[j] == '\0') return true;
	if (str[i] == '\0'&&pattern[j] != '\0' || str[i] != '\0'&&pattern[j] == '\0') return false;
	if ((str[i] == pattern[j]|| pattern[j] == '.') && pattern[j + 1]=='*') {
		return check(str,pattern,i + 1,j + 2)|| check(str, pattern, i + 1, j)||check(str, pattern, i, j + 2);
	}
	else if((str[i] == pattern[j] || pattern[j] == '.') && pattern[j + 1] != '*'){
		return check(str, pattern, i + 1, j + 1);
	}
	else if (pattern[j + 1] == '*') {
		return check(str,pattern,i,j + 2);
	}
	else return false;
}

bool match(char* str,char* pattern) {
	if (str == NULL || pattern == NULL) return false;
	return check(str, pattern,0, 0);
}

int main() {
	char *p = "aaa";
	char *q = "ab*ac*a";
	char *d = "ab*a";
	cout<< (match(p,q)==true) <<endl;
	cout << (match(p, d) == true) << endl;
	return 0;
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页