AcWing 1208. 翻硬币
原题链接
简单
翻硬币
#include<iostream>
#include<cstring>
using namespace std;
const int INF = 1e6;
const int N = 110;
char g[N], gg[N];
int dx[] = {0, -1};
int t;
void turn(int x) {
for(int i = 0; i < 2; i ++) {
int a = dx[i] + x;
if(a >= 0 && a < t / 2) { // 范围一开始没划好
if(g[a] == '*') g[a] = 'o';
else g[a] = '*';
}
}
}
int work() {
int ans = INF;
for(int k = 0; k < 2; k ++) {
char backup[N];
int res = 0;
memcpy(backup, g, t / 2);
for(int i = 0; i < 2; i ++)
if(k >> i && 1 ) {
res ++;
turn(i);
}
for(int i = 0; i < t / 2 - 1; i ++)
if(g[i] != g[i + (t / 2)]) {
res ++;
turn (i + 1);
}
if(g[t / 2 - 1] == g[t - 1]) ans = min(ans, res);
memcpy(g, backup, t / 2);
}
return ans;
}
int main() {
char x;
while(cin >> x) g[t ++] = x;
cout << work() << endl;
return 0;
}