//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ここまで fn convert(a: Vec) -> Vec<(char, u8)> { let mut s = vec![]; let mut i = 0; while i < a.len() { if i + 1 < a.len() && a[i + 1] == '?' { s.push((a[i], 1)); i += 2; } else if i + 1 < a.len() && a[i + 1] == '*' { s.push((a[i], 2)); i += 2; } else { s.push((a[i], 0)); i += 1; } } s } use std::cmp::min; fn run() { input! { a: chars, b: chars, } let a = convert(a); let b = convert(b); let mut dp = vec![vec![0; b.len() + 1]; a.len() + 1]; for i in 1..=a.len() { if a[i - 1].1 > 0 { dp[i][0] = dp[i - 1][0]; } else { dp[i][0] = dp[i - 1][0] + 1; } } for i in 1..=b.len() { if b[i - 1].1 > 0 { dp[0][i] = dp[0][i - 1]; } else { dp[0][i] = dp[0][i - 1] + 1; } } for i in 1..=a.len() { for j in 1..=b.len() { let x = a[i - 1]; let y = b[j - 1]; dp[i][j] = min(dp[i - 1][j] + 1, min(dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1)); if x.1 > 0 { dp[i][j] = min(dp[i][j], dp[i - 1][j]); } if y.1 > 0 { dp[i][j] = min(dp[i][j], dp[i][j - 1]); } if x.0 == y.0 { dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]); if x.1 == 2 { dp[i][j] = min(dp[i][j], dp[i][j - 1]); } if y.1 == 2 { dp[i][j] = min(dp[i][j], dp[i - 1][j]); } } } } println!("{}", dp[a.len()][b.len()]); } fn main() { run(); }