#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define MT make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)<; using vi = vector; using vll = vector; const int INF = INT_MAX / 3; string S, T; int dp[2002][2002]; void solve() { cin >> S >> T; int N = sz(S), M = sz(T); S += 'c'; T += 'd'; rep(i, N + 1)rep(j, M + 1)dp[i][j] = INF; dp[0][0] = 0; rep(i, N+1)rep(j, M+1) { int x = dp[i][j]; char a = S[i], b = T[j]; if ((a == '*' || a == '?') && (b == '*'&&b == '?')) { smin(dp[i + 1][j], dp[i - 1][j]); smin(dp[i][j + 1], dp[i][j - 1]); smin(dp[i + 1][j], x); smin(dp[i][j + 1], x); } else if (a == '*') { if (S[i - 1] == b) { smin(dp[i][j + 1], x); } smin(dp[i + 1][j], x); smin(dp[i + 1][j], dp[i - 1][j]); smin(dp[i][j + 1], x + 1); } else if (a == '?') { smin(dp[i + 1][j], x); smin(dp[i + 1][j], dp[i - 1][j]); } else if (b == '*') { if (a == T[j - 1]) { smin(dp[i + 1][j], x); } smin(dp[i][j + 1], x); smin(dp[i][j + 1], dp[i][j - 1]); smin(dp[i + 1][j], x + 1); } else if (b == '?') { smin(dp[i][j + 1], x); smin(dp[i][j + 1], dp[i][j - 1]); } else { smin(dp[i + 1][j], x + 1); smin(dp[i][j + 1], x + 1); smin(dp[i + 1][j + 1], x + 1); if (S[i] == T[j]) { smin(dp[i + 1][j + 1], x); } } } cout << dp[N][M] << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); solve(); return 0; }