第十一周算法设计与分析leetcode作业

本文详细解析了LeetCode上的难题“通配符匹配”,通过动态规划的方法解决输入字符串与模式匹配的问题,支持‘?’和‘*’两种通配符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. Wildcard Matching

https://leetcode.com/problems/wildcard-matching/description/
Difficulty:Hard
Total Accepted:148.7K
Total Submissions:680.8K

Given an input string (s) and a pattern §, implement wildcard pattern matching with support for ‘?’ and ‘*’.

'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).
The matching should cover the entire input string (not partial).

Note:

  • s could be empty and contains only lowercase letters a-z.
  • p could be empty and contains only lowercase letters a-z, and characters like ? or *.
    Example 1:
Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

Input:
s = "aa"
p = "*"
Output: true
Explanation: '*' matches any sequence.

Example 3:

Input:
s = "cb"
p = "?a"
Output: false
Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'.

Example 4:

Input:
s = "adceb"
p = "*a*b"
Output: true
Explanation: The first '*' matches the empty sequence, while the second '*' matches the substring "dce".

Example 5:

Input:
s = "acdcb"
p = "a*c?b"
Output: false

算法一看就比较像可以用动态规划解决的问题,因为状态比较好划分而且状态与状态之间可以画图连线找关系,因此就决定用DP方式解决。

思考过程如下:

  • 按照套路,用result[i][j]来存取p的前i个和s的前j个字符串的答案。
  • 假设result[i][j]与相邻的result[i - 1][j - 1],result[i - 1][j]和result[i][j - 1]有某种神奇的关系。因此有必要先求result的第一行和第一列的初始值,毕竟后面的推导要用到这两排的数值。
  • result的第一行除了第一个是true其他都是false, 很好理解,s的前i个(i>0)当然和空字符匹配不了。
  • result的第一列,如果p[i]为*,则它与上一行的值相同。因为 *是自由变换的符号。否则初始为false。
  • 那么其他行其他列呢?
  • 可以看到,因为当p[i]为*时变换最为自由,所以只要result[i - 1][j - 1],result[i - 1][j]和result[i][j - 1]之中有一个为true那么result[i][j ]也为true。
  • 如果p[i]==s[j]或者p[i]等于?,这意味着p和s的最后一个字符匹配,因此其结果result[i][j ]与result[i-1][j-1]是一样的,当做去掉s和p的最后一个字符考虑。

综上,就可以写出下面的程序了~

class Solution {
public:
	bool isMatch(string s, string p) {
		int resultX = p.length() + 1;
		int resultY = s.length() + 1;
		bool **result = new bool*[resultX];
		for (int i = 0; i < resultX; i++) {
			result[i] = new bool[resultY];
			for (int j = 0; j < resultY; j++) {
				result[i][j] = false;
			}
		}
		//result[i][j]表示p前i个和s前j个是否匹配
		//先初始化第一行除了第一个其他为false;
		result[0][0] = true;
		//先初始化第一列
		for (int i = 1; i < resultX; i++) {
			if (p[i - 1] == '*') {
				result[i][0] = result[i-1][0];
			}
		}

		for (int i = 1; i < resultX; i++) {
			for (int j = 1; j < resultY; j++) {
				if (s[j - 1] == p[i - 1]||p[i - 1] == '?') {//如果结尾相同或为?则看左上角对应的位置的值
					result[i][j] = result[i-1][j-1];
				}
				else if(p[i-1]=='*'){//看左边,左斜上角和上边的值
					if(result[i-1][j-1]==true||result[i-1][j]==true||result[i][j-1]==true){
						result[i][j] = true;
					}
				}	
			}
		}
		return result[resultX-1][resultY-1];
	}
};
static const auto io_sync_off = []()
{
	// turn off sync
	std::ios::sync_with_stdio(false);
	// untie in/out streams
	std::cin.tie(nullptr);
	return nullptr;
}();

国科大的算法设计分析相关1-5章复习题 第一章样例: 1.讲义习题一: 第1(执行步改为关键操作数)、第2、3、6、7题 习题一 1答:执行步4pmn+3pm+2m+1;关键操作2n*m*p 2方法一答:2n-2次 方法二答:2n-2次 3 1)证明:任给c,n>c,则10n2>cn 。不存在c使10n22c时,logn>c,从而n2logn>=cn2,同上。 6 答:logn,n2/3,20n,4n2,3n,n! 7 答:1)6+n 2) 3)任意n 2.讲义习题二:第5题。 答:c、e是割点。每点的DFN、L值:A1,1、B2,1、C3,1、D4,4、E5,1、F6,5、G7,5。最大连通分支CD、EFG、ABCE。 3.考虑下述选择排序算法: 输入:n个不等的整数的数组A[1..n] 输出:按递增次序排序的A For i:=1 to n-1 For j:=i+1 to n If A[j]<A[i] then A[i] A[j] 问:(1)最坏情况下做多少次比较运算?答1+2+..+n-1=n(n-1)/2 (2)最坏情况下做多少次交换运算?在什么输入时发生? n(n-1)/2,每次比较都交换,交换次数n(n-1)/2。 4.考虑下面的每对函数f(n)和g(n) ,比较他们的阶。 (1) f(n)=(n2-n)/2, g(n)=6n (2)f(n)=n+2 , g(n)=n2 (3)f(n)=n+nlogn, g(n)=n (4)f(n)=log(n!), g(n)= 答:(1)g(n)=O(f(n)) (2)f(n)=O(g(n) (3)f(n)=O(g(n) (4)f(n)=O(g(n) 5.在表中填入true或false . 答案: f(n) g(n) f(n)=O(g(n) f(n)=(g(n)) f(n)=(g(n)) 1 2n3+3n 100n2+2n+100 F T F 2 50n+logn 10n+loglogn T T T 3 50nlogn 10nloglogn F T F 4 logn Log2n T F F 5 n! 5n F T F 6.用迭代法求解下列递推方程: (1) (2) ,n=2k 答:(1)T(n)=T(n-1)+n-1=T(n-2)+n-2+n-1 =…=T(1)+1+2+…+n-1=n(n-1)/2=O(n2) (2)T(n)=2T(n/2)+n-1=2(2T(n/4)+n/2-1)+n-1 =4T(n/4)+n-2+n-1=4(2T(n/23)+n/4-1)+n-2+n-1 =23T(n/23)+n-4+n-2+n-1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值