-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo10_Dp.cs
123 lines (115 loc) · 4.44 KB
/
No10_Dp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
using System.Text;
namespace LeetCode_10
{
//static void Main(string[] args)
//{
// var solution = new Solution();
// while (true)
// {
// //int input = int.Parse(Console.ReadLine());
// //int input2 = int.Parse(Console.ReadLine());
// //int input3 = int.Parse(Console.ReadLine());
// //string input = Console.ReadLine();
// //string input = "A man, a plan, a canal: Panama";
// //string input2 = Console.ReadLine();
// //int[] intArr = input.Split(',').Select(s => int.Parse(s)).ToArray();
// //int input2 = int.Parse(Console.ReadLine());
// //int[] intArr = new int[] { 1, 3, 2 };
// //int[] intArr = new int[] { 1, 3 };
// string input = "aab";
// string input2 = "c*a*b";
// var res = solution.IsMatch(input, input2);
// ConsoleX.WriteLine(res);
// }
//}
/// <summary>
/// REDO
/// </summary>
class Solution
{
/// <summary>
/// REVIEW
/// 2020.08.04: 重新做了一遍,比上次好很多了,能动手做了,但是状态转移的推导还是很困难。多个匹配是个难点,在那里卡了相当久
/// </summary>
public bool IsMatch(String s, String p)
{
//二维数组代表两个字符串的前多少个字符是否匹配,多申请一位避免出界
bool[,] dp = new bool[s.Length + 1, p.Length + 1];
//作为初始的匹配状态,用于状态转移
dp[0, 0] = true;
for (int i = 0; i <= s.Length; i++)
{
for (int j = 1; j <= p.Length; j++)
{
if (p[j - 1] == '*')
//这里的 i - 2 >= 0 判断其实可以去掉,因为根据题意,不可能出现直接 * 开头的
dp[i, j] = ((j - 2 >= 0 ? IsMatchAtSpecificPos(s, p, i - 1, j - 2) : false) && dp[i - 1, j])//这个多个匹配的状态转移很重要,以这个为基础也推出了为什么 i 要从 0 开始。
|| (j - 2 >= 0 ? dp[i, j - 2] : false);
else
{
dp[i, j] = IsMatchAtSpecificPos(s, p, i - 1, j - 1) && dp[i - 1, j - 1];
}
}
}
return dp[s.Length, p.Length];
}
private bool IsMatchAtSpecificPos(string s, string p, int sIndex, int pIndex)
{
if (sIndex == -1)
return false;
if (p[pIndex] == '.')
return true;
return s[sIndex] == p[pIndex];
}
/// <summary>
/// 动态规划,要仔细分析状态,然后写好转移方程才行。而且具体编码需要注意很多细节
/// Experience:高级方法对于低级方法来说就是降维打击,这道题有限确定状态机可以做,但是相较于动态规划而言就是刀耕火种,完全可以舍弃掉。
/// </summary>
/// <param name="s"></param>
/// <param name="p"></param>
/// <returns></returns>
//public bool IsMatch(String s, String p)
//{
// int m = s.Length;
// int n = p.Length;
// bool[,] f = new bool[m + 1, n + 1];
// f[0, 0] = true;
// for (int i = 0; i <= m; ++i)
// {
// for (int j = 1; j <= n; ++j)
// {
// if (p[j - 1] == '*')
// {
// f[i, j] = f[i, j - 2];
// if (matches(s, p, i, j - 1))
// {
// f[i, j] = f[i, j] || f[i - 1, j];
// }
// }
// else
// {
// if (matches(s, p, i, j))
// {
// f[i, j] = f[i - 1, j - 1];
// }
// }
// }
// }
// return f[m, n];
//}
//public bool matches(String s, String p, int i, int j)
//{
// if (i == 0)
// {
// return false;
// }
// if (p[j - 1] == '.')
// {
// return true;
// }
// return s[i - 1] == p[j - 1];
//}
}
}