题解
直接按题意模拟即可,如果当前位置和目标位置相同,则跳过,否则将当前位置和下一个位置进行翻转,因为题目中保证了有解,所以我们不需要考虑类似***o
和*o*o
的情况。
#include <bits/stdc++.h>
using namespace std;
void change(char &ch) { // 传引用,类似指针,可以直接修改变量的值。
if(ch == '*') ch = 'o';
else ch = '*';
}
int main() {
string s1, s2;
cin >> s1 >> s2;
int tot = 0;
for(int i = 0; i < s1.size() - 1; i++) {
if(s1[i] != s2[i]) {
change(s1[i]), change(s1[i + 1]);
tot++;
}
}
cout << tot << endl;
return 0;
}
使用C++的bitset来进行操作:
#include <bits/stdc++.h>
using namespace std;
void change_binary(string &s1) {
for(auto &x: s1) {
if(x == '*') x = '1';
else x = '0';
}
}
int main() {
string s1, s2;
cin >> s1 >> s2;
change_binary(s1), change_binary(s2); // 将字符串变成二进制字符串
bitset<128> bs1(s1), bs2(s2), fac("1"); // 因为字符串长度不超过100位,所以我们设置为128位二进制
int tot = 0;
for(int i = 0; i < bs1.size() - 1; i++) {
if(bs1[i] != bs2[i]) {
bs1 ^= ((fac << i + 1) | (fac << i)); // 右边式子是把当前位置和下一位置成1,然后进行异或运算,相同为0,不同为1,这样就变成了翻转的过程了。
tot++;
}
}
cout << tot << endl;
}