题目来自DOTCPP:
思路:
①题目中说“一个棋子和它两边的棋子都不一样,就可以将其翻转变成另一个颜色”,因此首尾是不能翻转的。
②我们从左到右枚举,只要T[i] != S[i] ,判断S[i-1] 和 S[i+1] 和 S[i] 的是否相等,不相等,就可以变换 S[i],答案++。
③因为是多组数据,所以记录答案的ans,每次都要初始化。
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6+20;
int n;
signed main(){
cin >> n;
while(n--){
string t, s;cin >> t; cin >> s;
int ans = 0;
for(int i = 1; i < (int)t.size()-1; i++){
if(t[i] != s[i]){
if(s[i-1] != s[i] && s[i] != s[i+1]){
s[i] = t[i];
ans +=1;
}
}
}
if(t == s){
cout << ans << endl;
}else{
cout << "-1" << endl;
}
}
return 0;
}