# 通配符匹配

<p>给定一个字符串&nbsp;(<code>s</code>) 和一个字符模式&nbsp;(<code>p</code>) ，实现一个支持&nbsp;<code>&#39;?&#39;</code>&nbsp;和&nbsp;<code>&#39;*&#39;</code>&nbsp;的通配符匹配。</p><pre>&#39;?&#39; 可以匹配任何单个字符。&#39;*&#39; 可以匹配任意字符串（包括空字符串）。</pre><p>两个字符串<strong>完全匹配</strong>才算匹配成功。</p><p><strong>说明:</strong></p><ul>	<li><code>s</code>&nbsp;可能为空，且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母。</li>	<li><code>p</code>&nbsp;可能为空，且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母，以及字符&nbsp;<code>?</code>&nbsp;和&nbsp;<code>*</code>。</li></ul><p><strong>示例&nbsp;1:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong> &quot;a&quot; 无法匹配 &quot;aa&quot; 整个字符串。</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;*&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;&#39;*&#39; 可以匹配任意字符串。</pre><p><strong>示例&nbsp;3:</strong></p><pre><strong>输入:</strong>s = &quot;cb&quot;p = &quot;?a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong>&nbsp;&#39;?&#39; 可以匹配 &#39;c&#39;, 但第二个 &#39;a&#39; 无法匹配 &#39;b&#39;。</pre><p><strong>示例&nbsp;4:</strong></p><pre><strong>输入:</strong>s = &quot;adceb&quot;p = &quot;*a*b&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;第一个 &#39;*&#39; 可以匹配空字符串, 第二个 &#39;*&#39; 可以匹配字符串 &quot;dce&quot;.</pre><p><strong>示例&nbsp;5:</strong></p><pre><strong>输入:</strong>s = &quot;acdcb&quot;p = &quot;a*c?b&quot;<strong><br />输出:</strong> false</pre>

以下程序实现了这一功能，请你填补空白处的内容：

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static bool isMatch(char *s, char *p)
{
	char *last_s = NULL;
	char *last_p = NULL;
	while (*s != '\0')
	{
		if (*p == '*')
		{
			if (*++p == '\0')
			{
				return true;
			}
			last_s = s;
			last_p = p;
		}
		_____________________;
		else
		{
			return false;
		}
	}
	while (*p == '*')
	{
		p++;
	}
	return *p == '\0';
}
int main(int argc, char **argv)
{
	if (argc != 3)
	{
		fprintf(stderr, "Usage: ./test string pattern\n");
		exit(-1);
	}
	printf("%s\n", isMatch(argv[1], argv[2]) ? "true" : "false");
	return 0;
}
```

## template

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static bool isMatch(char *s, char *p)
{
	char *last_s = NULL;
	char *last_p = NULL;
	while (*s != '\0')
	{
		if (*p == '*')
		{
			if (*++p == '\0')
			{
				return true;
			}
			last_s = s;
			last_p = p;
		}
		else if (*p == '?' || *s == *p)
		{
			s++;
			p++;
		}
		else if (last_s != NULL)
		{
			p = last_p;
			s = ++last_s;
		}
		else
		{
			return false;
		}
	}
	while (*p == '*')
	{
		p++;
	}
	return *p == '\0';
}
int main(int argc, char **argv)
{
	if (argc != 3)
	{
		fprintf(stderr, "Usage: ./test string pattern\n");
		exit(-1);
	}
	printf("%s\n", isMatch(argv[1], argv[2]) ? "true" : "false");
	return 0;
}
```

## 答案

```cpp
else if (*p == '?' || *s == *p)
{
	s++;
	p++;
}
else if (last_s != NULL)
{
	p = last_p;
	s = ++last_s;
}
```

## 选项

### A

```cpp
else if (last_s != NULL)
{
	p = last_p;
	s = ++last_s;
}
```

### B

```cpp
else if (*p == '?' || *s == *p)
{
	s++;
	p++;
}
```

### C

```cpp
else if (*p == '?' && *s == *p)
{
	s++;
	p++;
}
else if (last_s != NULL)
{
	p = last_p;
	s = ++last_s;
}
```