AcWing 1208. 翻硬币(传送门)
思路分析:
这个题也是个纸老虎。每次只能翻转相邻的两个,题目又保证有解,我的想法就是,两两一对进行计算,话不多说,上代码!
AC代码:
#include <iostream>
using namespace std;
string s;
string w;
int main() {
cin >> s >> w;
int res = 0;
for (int i = 0; i < s.size(); i++)
if (s[i] != w[i])
for (int j = i + 1;; j++) {
res++;
if (s[j] != w[j]) {
i = j;
break;
}
}
cout << res << endl;
return 0;
}
后来又听了一种题解,是递推的思想,本质上和我的一样QWQ,有点模拟的味道,来一起欣赏一下叭~
AC代码:
#include <iostream>
using namespace std;
string a, b;
void turn(int i) {
if (a[i] == '*')
a[i] = 'o';
else
a[i] = '*';
}
int main() {
cin >> a >> b;
int res = 0;
for (int i = 0; i + 1 < a.size(); i++)
if (a[i] != b[i]) {
res++;
turn(i);
turn(i + 1);
}
cout << res << endl;
return 0;
}