C++
$\color{gold}{— > 蓝桥杯辅导课题解}$
思路:
$从前往后遍历一遍原串start,有跟目标串 aim 不同的直接翻转一下即可$
时间复杂度:$O(n)$
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 110;
char start[N], aim[N];
void turn(int x) {
if (start[x] == '*') start[x] = 'o';
else start[x] = '*';
}
int main() {
cin >> start >> aim;
int n = strlen(start);
int res = 0;
for (int i = 0; i < n - 1; i ++)
if (start[i] != aim[i]) {
turn(i), turn(i + 1); // 翻转
res ++;
}
cout << res;
return 0;
}
Orz
orz