-
Notifications
You must be signed in to change notification settings - Fork 111
/
palindrome-partitioning.rs
95 lines (91 loc) · 3.01 KB
/
palindrome-partitioning.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
impl Solution {
pub fn partition(s: String) -> Vec<Vec<String>> {
#[derive(Default)]
struct C<'a> { s: &'a str, r0: Vec<usize>, r1: Vec<usize>, ret: Vec<Vec<String>>, r: Vec<String> }
impl<'a> C<'a> {
fn dfs(&mut self, i: usize) {
if i == self.s.len() {
self.ret.push(self.r.clone());
return;
}
for j in 1..=self.s.len()-i {
if (if j%2!=0 { &self.r1 } else { &self.r0 })[i+j/2] >= (j+1)/2 {
self.r.push(self.s[i..i+j].to_string());
self.dfs(i+j);
self.r.pop();
}
}
}
}
let a = s.as_bytes(); let n = a.len();
let mut f = 0; let mut g = 0;
let mut c = C { s: &s, r0: vec![0; n], r1: vec![0; n], ..Default::default() };
for i in 0..n {
c.r1[i] = if i < g && c.r1[2*f-i] != g-i { c.r1[2*f-i].min(g-i) } else {
f = i;
g = g.max(i);
while g < n && 2*f >= g && a[2*f-g] == a[g] { g += 1; }
g-f
}
}
g = 0;
for i in 0..n {
c.r0[i] = if i < g && c.r0[2*f-i] != g-i { c.r0[2*f-i].min(g-i) } else {
f = i;
g = g.max(i);
while g < n && 2*f >= g+1 && a[2*f-1-g] == a[g] { g += 1; }
g-f
}
}
c.dfs(0);
c.ret
}
}
///
impl Solution {
pub fn partition(s: String) -> Vec<Vec<String>> {
fn dfs<'a,'b>(s: &'a str, path: &'a [Vec<usize>], i: usize, ret: &'b mut Vec<Vec<String>>, r: &'b mut Vec<&'a str>) {
if i == s.len() {
ret.push(r.iter().map(|x| x.to_string()).collect());
return;
}
for &j in path[i].iter() {
r.push(&s[i..j]);
dfs(s, path, j, ret, r);
r.pop();
}
}
let a = s.as_bytes(); let n = a.len();
let mut f = 0; let mut g = 0;
let mut r0 = vec![0; n];
let mut r1 = vec![0; n];
let mut path = vec![vec![]; n];
for i in 0..n {
r1[i] = if i < g && r1[2*f-i] != g-i { r1[2*f-i].min(g-i) } else {
f = i;
g = g.max(i);
while g < n && 2*f >= g && a[2*f-g] == a[g] { g += 1; }
g-f
};
for j in 0..r1[i] {
path[i-j].push(i+j+1);
}
}
g = 0;
for i in 0..n {
r0[i] = if i < g && r0[2*f-i] != g-i { r0[2*f-i].min(g-i) } else {
f = i;
g = g.max(i);
while g+1 < n && 2*f >= g && a[2*f-g] == a[g+1] { g += 1; }
g-f
};
for j in 0..r0[i] {
path[i-j].push(i+j+2);
}
}
let mut ret = vec![];
let mut r = vec![];
dfs(&s, &path, 0, &mut ret, &mut r);
ret
}
}