Skip to content

Commit b24bd3b

Browse files
committed
match 184
1 parent 975f92f commit b24bd3b

File tree

5 files changed

+225
-0
lines changed

5 files changed

+225
-0
lines changed

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,10 @@ mod prob_5367;
408408
mod prob_5370;
409409
mod prob_5377;
410410
mod prob_5379;
411+
mod prob_5380;
412+
mod prob_5381;
413+
mod prob_5382;
414+
mod prob_5383;
411415
mod prob_5437;
412416
mod sword_offer;
413417
mod crack_code_interview;

src/prob_5380.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
impl Solution {
2+
pub fn string_matching(words: Vec<String>) -> Vec<String> {
3+
let n = words.len();
4+
let mut ans = vec![];
5+
for i in 0..n {
6+
for j in 0..n {
7+
if j != i && words[i].len() < words[j].len() {
8+
if words[j].contains(&words[i]) {
9+
ans.push(words[i].clone());
10+
break;
11+
}
12+
}
13+
}
14+
}
15+
ans
16+
}
17+
}
18+
19+
struct Solution;
20+
21+
#[cfg(test)]
22+
mod tests {
23+
use super::Solution;
24+
25+
#[test]
26+
fn test_string_matching() {
27+
let test_cases = vec![
28+
(vec!["mass","as","hero","superhero"], vec!["as", "hero"]),
29+
];
30+
for (arr, expect) in test_cases {
31+
assert_eq!(Solution::string_matching(arr.iter().map(|v| v.to_string()).collect()), expect);
32+
}
33+
}
34+
}

src/prob_5381.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
pub fn process_queries(queries: Vec<i32>, m: i32) -> Vec<i32> {
3+
let m = m as usize;
4+
let mut arr = vec![0; m];
5+
for i in 0..m {
6+
arr[i] = (i+1) as i32;
7+
}
8+
let mut ans = vec![];
9+
for q in queries {
10+
for i in 0..m {
11+
if arr[i] == q {
12+
ans.push(i as i32);
13+
for j in (0..i).rev() {
14+
arr[j+1] = arr[j];
15+
}
16+
arr[0] = q;
17+
}
18+
}
19+
}
20+
ans
21+
}
22+
}
23+
24+
struct Solution;

src/prob_5382.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
impl Solution {
2+
pub fn entity_parser(text: String) -> String {
3+
if text.is_empty() {
4+
return text;
5+
}
6+
let text = text.as_bytes();
7+
let mut ans = vec![];
8+
let quot = "quot;";
9+
let apos = "apos;";
10+
let amp = "amp;";
11+
let gt = "gt;";
12+
let lt = "lt;";
13+
let frasl = "frasl;";
14+
let n = text.len();
15+
let mut i = 0;
16+
while i < n {
17+
if text[i] != b'&' {
18+
ans.push(text[i]);
19+
i+=1;
20+
continue;
21+
}
22+
if i+3 < n {
23+
if text[i+1..i+4].eq(gt.as_bytes()) {
24+
ans.push(b'>');
25+
i+=4;
26+
continue;
27+
}
28+
if text[i+1..i+4].eq(lt.as_bytes()) {
29+
ans.push(b'<');
30+
i+=4;
31+
continue;
32+
}
33+
}
34+
if i+4 < n {
35+
if text[i+1..i+5].eq(amp.as_bytes()) {
36+
ans.push(b'&');
37+
i+=5;
38+
continue;
39+
}
40+
}
41+
if i+5 < n {
42+
if text[i+1..i+6].eq(quot.as_bytes()) {
43+
ans.push(b'"');
44+
i+=6;
45+
continue;
46+
}
47+
if text[i+1..i+6].eq(apos.as_bytes()) {
48+
ans.push(b'\'');
49+
i+=6;
50+
continue;
51+
}
52+
}
53+
if i+6 < n {
54+
if text[i+1..i+7].eq(frasl.as_bytes()) {
55+
ans.push(b'/');
56+
i+=7;
57+
continue;
58+
}
59+
}
60+
ans.push(b'&');
61+
i+=1;
62+
}
63+
unsafe {std::str::from_utf8_unchecked(&ans).to_string()}
64+
}
65+
}
66+
67+
struct Solution;

src/prob_5383.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const MOD: i64 = 1000000007;
2+
3+
impl Solution {
4+
pub fn num_of_ways(n: i32) -> i32 {
5+
if n == 1 {
6+
return 12;
7+
}
8+
let mut available = vec![];
9+
for i in 1..64usize {
10+
let c1 = i & 3;
11+
let c2 = (i>>2)&3;
12+
let c3 = (i>>4)&3;
13+
if c1 !=0 && c2 != 0 && c3 != 0 && c3 != c2 && c2 != c1 {
14+
available.push(i);
15+
}
16+
}
17+
assert_eq!(12, available.len());
18+
let mut count = [0; 64];
19+
for &v in &available {
20+
count[v as usize] = 1i64;
21+
}
22+
for _ in 1..n {
23+
let mut new_count = [0; 64];
24+
for &pre_color in &available {
25+
let num = count[pre_color];
26+
if num == 0 {
27+
continue;
28+
}
29+
let c1 = pre_color & 3;
30+
let c2 = (pre_color>>2)&3;
31+
let c3 = (pre_color>>4)&3;
32+
for &color in &available {
33+
let t1 = color & 3;
34+
let t2 = (color>>2)&3;
35+
let t3 = (color>>4)&3;
36+
if t1 != c1 && t2 != c2 && t3 != c3 {
37+
new_count[color] += num;
38+
new_count[color] %= MOD;
39+
}
40+
}
41+
}
42+
count = new_count;
43+
}
44+
((count.iter().sum::<i64>()) % MOD) as i32
45+
}
46+
pub fn num_of_ways_mat(n: i32) -> i32 {
47+
if n == 1 {
48+
return 12;
49+
}
50+
let ((a,b),(c,d)) = Self::mat_pow(((3,2), (2,2)), n as usize-1);
51+
((6*(a+b+c+d)) % MOD) as i32
52+
}
53+
fn mat_pow(mat: ((i64, i64), (i64,i64)), n: usize) -> ((i64, i64), (i64,i64)) {
54+
if n == 1 {
55+
return mat;
56+
}
57+
let t = n / 2;
58+
let q= Self::mat_pow(mat, t);
59+
let w = Self::mat_mul(q, q);
60+
if n & 1 == 1 {
61+
Self::mat_mul(w, mat)
62+
} else {
63+
w
64+
}
65+
}
66+
fn mat_mul(x: ((i64, i64), (i64,i64)), y: ((i64, i64), (i64,i64))) -> ((i64, i64), (i64,i64)) {
67+
let ((x11, x12),(x21,x22)) = x;
68+
let ((y11,y12), (y21,y22)) = y;
69+
let z11 = (x11*y11+x12*y21) % MOD;
70+
let z12 = (x11*y12+x12*y22) % MOD;
71+
let z21 = (x21*y11+x22*y21) % MOD;
72+
let z22 = (x21*y21+x22*y22) % MOD;
73+
((z11, z12), (z21, z22))
74+
}
75+
}
76+
77+
struct Solution;
78+
79+
#[cfg(test)]
80+
mod tests {
81+
use super::Solution;
82+
83+
#[test]
84+
fn test_num_of_ways() {
85+
let test_cases = vec![
86+
(2, 54),
87+
(3, 246),
88+
(7, 106494),
89+
(5000, 30228214),
90+
];
91+
for (n, expect) in test_cases {
92+
//assert_eq!(Solution::num_of_ways(n), expect, "n:{}", n);
93+
assert_eq!(Solution::num_of_ways_mat(n), expect, "mat, n: {}", n);
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)