-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.rs
163 lines (147 loc) · 5.38 KB
/
main.rs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
fn main() {
assert_eq!(Solution::is_match(String::from("bbbababbabbbbabbbbaabaaabbbbabbbababbbbababaabbbab"), String::from("a******b*")), false);
}
struct Solution {}
impl Solution {
pub fn is_match_n(s: String, p: String) -> bool {
let mut dp = vec![vec![-1; p.len() + 1]; s.len() + 1];
dp[0][0] = 1;
let mut s: Vec<char> = s.chars().collect();
s.insert(0, ' ');
let mut p: Vec<char> = p.chars().collect();
p.insert(0, ' ');
for j in 1..p.len() {
if p[j] != '*' { break }
else { dp[0][j] = 1 }
}
for j in 1..p.len() {
dp[0][j] = if dp[0][j] <= 0 { 0 } else { 1 }
}
for i in 1..s.len() {
dp[i][0] = 0;
}
let mut x = Dp::new(&s, &p, &mut dp);
let ret = if x.cal(s.len()-1, p.len()-1) > 0 { true } else { false };
println!("x.dp = {:#?}", x.dp);
ret
}
pub fn is_match_g(s: String, p: String) -> bool {
if s.is_empty() && p.is_empty() { return true }
else if !s.is_empty() && p.is_empty() { return false }
let s: Vec<char> = s.chars().collect();
let mut p: Vec<char> = p.chars().collect();
if s.is_empty() {
p.dedup();
if p == vec!['*'] { return true }
else { return false }
}
// now s and p are both not empty
let (mut i, mut j) = (0, 0);
let (mut is, mut js) = (None, None); // save i j
while i < s.len() {
if j < p.len() && (p[j] == '?' || s[i] == p[j]) {
i += 1;
j += 1;
} else if j < p.len() && p[j] == '*' {
is = Some(i);
js = Some(j);
j += 1;
} else if js != None {
i = is.unwrap() + 1;
is = Some(i);
j = js.unwrap() + 1;
} else {
return false
}
}
while j < p.len() && p[j] == '*' { j += 1 }
if j == p.len() && p[j-1] == '*' { return true }
if j == p.len() { true } else { false }
}
pub fn is_match<S: AsRef<str>>(s: S, p: S) -> bool {
let (s, p) = (s.as_ref().as_bytes(), p.as_ref().as_bytes());
if s.is_empty() { return p.is_empty() || p.iter().all(|x| *x == b'*'); }
else if p.is_empty() { return false; }
let mut dp = vec![false; p.len()+1];
dp[0] = true;
for j in 1..dp.len() {
dp[j] = if p[j-1] == b'*' { dp[j-1] } else { break };
}
for i in 1..=s.len() {
let mut dp_i_1_j_1 = dp[0];
for j in 1..dp.len() {
let saved = dp[j];
dp[j] = if s[i-1] == p[j-1] || p[j-1] == b'?' { dp_i_1_j_1 }
else if p[j-1] == b'*' { dp[j] || dp[j-1] }
else { false };
dp_i_1_j_1 = saved;
}
if i == 1 { dp[0] = false; }
}
*dp.last().unwrap()
}
}
struct Dp<'a> {
s: &'a [char],
p: &'a [char],
dp: &'a mut Vec<Vec<i32>>
}
impl<'a> Dp<'a> {
pub fn cal(&mut self, i:usize, j:usize) -> i32 {
if self.dp[i][j] >= 0 { return self.dp[i][j] }
self.dp[i][j] = match (self.s[i], self.p[j]) {
(_, '*') => {
if self.cal(i, j-1) == 1 { 1 }
else if self.cal(i-1, j) == 1 { 1 }
else if self.cal(i-1, j-1) == 1 { 1 }
else { 0 }
}
(_, '?') => self.cal(i-1, j-1),
(a, b) => {
if a == b { self.cal(i-1, j-1) }
else { 0 }
}
};
self.dp[i][j]
}
pub fn new(s: &'a [char], p: &'a [char], dp: &'a mut Vec<Vec<i32>>) -> Dp<'a> {
Dp { s,p,dp }
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic_t() {
assert_eq!(Solution::is_match(String::from("aa"), String::from("a")), false);
assert_eq!(Solution::is_match(String::from("cb"), String::from("?a")), false);
assert_eq!(Solution::is_match(String::from("acdcb"), String::from("a*c?b")), false);
}
#[test]
fn basic_f() {
assert_eq!(Solution::is_match(String::from("aa"), String::from("*")), true);
// assert_eq!(Solution::is_match(String::from("adceb"), String::from("*a*b")), true);
}
#[test]
fn basic() {
assert_eq!(Solution::is_match(String::from("abccssbsbsbsdbb"), String::from("a*b?cs*sdb?")), true);
assert_eq!(Solution::is_match(String::from("adbd"), String::from("*a******bd")), true);
}
#[test]
fn edge() {
assert_eq!(Solution::is_match(String::from("abccssbsbsbsdbb"), String::from("*")), true);
assert_eq!(Solution::is_match(String::from(""), String::from("****")), true);
assert_eq!(Solution::is_match(String::from(""), String::from("a****")), false);
assert_eq!(Solution::is_match(String::from(""), String::from("")), true);
assert_eq!(Solution::is_match(String::from("sdf"), String::from("")), false);
assert_eq!(Solution::is_match(String::from("bbbababbabbbbabbbbaabaaabbbbabbbababbbbababaabbbab"), String::from("a******b*")), false);
}
#[test]
fn w1a() {
assert_eq!(Solution::is_match(String::from("mississippi"), String::from("m??*ss*?i*pi")), false);
}
#[test]
fn wa2() {
assert_eq!(Solution::is_match(String::from("aa"), String::from("*a")), true);
}
}