题解:P13888 [蓝桥杯 2023 省 Python A] 翻转
__coderyc__ · · 题解
思路
遍历检查可翻转的位置。
仅在不同的时候进行必要的翻转。
操作完成,如果能得出结果,输出最少次数,否则判定无解。
时间复杂度为
代码
#include <iostream>
#include <string>
using namespace std;
void solve() {
int D;
cin >> D;
while (D--) {
string T, S;
cin >> T >> S;
int n = T.size();
int flips = 0;
bool possible = true;
for (int i = 1; i < n - 1; ++i) {
if (S[i-1] != S[i] && S[i] != S[i+1]) {
if (S[i] != T[i]) {
S[i] = T[i];
flips++;
}
}
}
if (S == T) {
cout << flips << endl;
} else {
cout << -1 << endl;
}
}
}
int main() {
solve();
return 0;
}