2472. 不重叠回文子字符串的最大数目
This commit is contained in:
+43
-19
@@ -5,29 +5,53 @@ mod list;
|
||||
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
fn check(img1: &Vec<Vec<i32>>, img2: &Vec<Vec<i32>>, dx: i32, dy: i32) -> i32 {
|
||||
let (length, width) = (img2.len() as i32, img2[0].len() as i32);
|
||||
let mut ans= 0;
|
||||
for i in 0..length {
|
||||
for j in 0..width {
|
||||
let x = i + dx;
|
||||
let y = j + dy;
|
||||
if x < 0 || y < 0 || x >= length || y>= width {
|
||||
pub fn max_palindromes(s: String, k: i32) -> i32 {
|
||||
let k = k as usize;
|
||||
let mut mat = vec![vec![false; s.len()]; s.len() + 1];
|
||||
let s = s.as_bytes();
|
||||
let (mut af1, mut af2) = (false, true);
|
||||
for i in 0..s.len() {
|
||||
mat[0][i] = true;
|
||||
mat[1][i] = i + 1 < s.len() && s[i] == s[i + 1];
|
||||
af2 &= mat[1][i];
|
||||
}
|
||||
for i in 2..s.len() {
|
||||
if i % 2 == 0 {
|
||||
if af1 {
|
||||
continue;
|
||||
}
|
||||
if img1[x as usize][y as usize] == 1 && img2[i as usize][j as usize] == 1 {
|
||||
ans += 1;
|
||||
} else if af2 {
|
||||
continue;
|
||||
}
|
||||
let mut local = false;
|
||||
for j in 0..s.len() - i {
|
||||
mat[i][j] = mat[i - 2][j + 1] && s[j] == s[j + i];
|
||||
local |= mat[i][j];
|
||||
}
|
||||
if i%2 == 0 {
|
||||
af1 = !local;
|
||||
}else {
|
||||
af2 = !local;
|
||||
}
|
||||
}
|
||||
// for i in 0..s.len() {
|
||||
// println!("{:?} ", mat[i]);
|
||||
// }
|
||||
let mut events = Vec::new();
|
||||
for i in k - 1 .. s.len() {
|
||||
for j in 0..s.len() - i {
|
||||
if mat[i][j] {
|
||||
events.push((j, j + i + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
ans
|
||||
}
|
||||
pub fn largest_overlap(img1: Vec<Vec<i32>>, img2: Vec<Vec<i32>>) -> i32 {
|
||||
let (len, width) = (img1.len() as i32, img1[0].len() as i32);
|
||||
let mut ans = 0;
|
||||
for i in -len + 1..len {
|
||||
for j in -width + 1..width {
|
||||
ans = ans.max(Self::check(&img1, &img2, i, j));
|
||||
events.sort_unstable_by(|a,b| a.1.cmp(&b.1));
|
||||
// println!("{:?}", events);
|
||||
let (mut certain,mut ans) = (0,0);
|
||||
for (a,b) in events {
|
||||
if a >= certain {
|
||||
certain = b;
|
||||
ans += 1;
|
||||
}
|
||||
}
|
||||
ans
|
||||
@@ -37,6 +61,6 @@ impl Solution {
|
||||
fn main() {
|
||||
println!(
|
||||
"{:?}",
|
||||
Solution::largest_overlap(make_matrix("[[1,1,0],[0,1,0],[0,1,0]]"), make_matrix("[[0,0,0],[1,0,1],[0,0,1]]"))
|
||||
Solution::max_palindromes("abaccdbbd".to_string(), 3)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user