点击查看代码
#include <bits/stdc++.h>
using namespace std;const int INF = 1e8; int main() {ios::sync_with_stdio(0); cin.tie(0);string s[2];// 【核心修正】题目没有单独输入 n!输入只有两行字符串if (!(cin >> s[0] >> s[1])) return 0;// 直接通过字符串长度获取 nint n = s[0].length();// 掐头去尾,寻找有效区间int L = 0, R = n - 1;while (L < n && s[0][L] == '.' && s[1][L] == '.') L++;while (R >= 0 && s[0][R] == '.' && s[1][R] == '.') R--;// 如果全都是 '.',不需要加任何检测器if (L > R) {cout << 0 << '\n';return 0;}int dp0 = INF, dp1 = INF, dp2 = INF;bool c0 = (s[0][L] == '#'), c1 = (s[1][L] == '#');// 初始化起点状态if (c0 && c1) {dp2 = 0;} else if (c0) {dp0 = 0; dp2 = 1;} else if (c1) {dp1 = 0; dp2 = 1;}// 核心 DP 滚动转移for (int i = L + 1; i <= R; ++i) {c0 = (s[0][i] == '#');c1 = (s[1][i] == '#');int cost0 = c1 ? INF : (1 - c0);int cost1 = c0 ? INF : (1 - c1);int cost2 = (1 - c0) + (1 - c1);int t0 = min(dp0, dp2) + cost0;int t1 = min(dp1, dp2) + cost1;int t2 = min({dp0, dp1, dp2}) + cost2;dp0 = min(t0, INF);dp1 = min(t1, INF);dp2 = min(t2, INF);}// 输出结果cout << min({dp0, dp1, dp2}) << '\n';return 0;
}
