generated from projeto-de-algoritmos-2024/RepositorioTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
49 lines (43 loc) · 1.55 KB
/
Program.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
using System;
class Program
{
static bool IsMatch(string s, string p)
{
int m = s.Length, n = p.Length;
bool[,] dp = new bool[m + 1, n + 1];
dp[0, 0] = true; // Strings vazias correspondem
// Preenchendo os casos onde * pode eliminar caracteres
for (int j = 1; j <= n; j++)
{
if (p[j - 1] == '*')
dp[0, j] = dp[0, j - 2]; // Remove o elemento antes de '*'
}
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (p[j - 1] == s[i - 1] || p[j - 1] == '.')
{
dp[i, j] = dp[i - 1, j - 1]; // Caracteres combinam diretamente
}
else if (p[j - 1] == '*')
{
dp[i, j] = dp[i, j - 2]; // Caso de remover o caractere antes de '*'
if (p[j - 2] == s[i - 1] || p[j - 2] == '.')
{
dp[i, j] = dp[i, j] || dp[i - 1, j]; // Caso de usar o '*' múltiplas vezes
}
}
}
}
return dp[m, n];
}
static void Main()
{
Console.WriteLine(IsMatch("aa", "a")); // false
Console.WriteLine(IsMatch("aa", "a*")); // true
Console.WriteLine(IsMatch("ab", ".*")); // true
Console.WriteLine(IsMatch("mississippi", "mis*is*p*.")); // false
Console.WriteLine(IsMatch("mississippi", "mis*is*ip*.")); // true
}
}