AcWing 寒假每日一题 2021-01-18 翻硬币

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;
}

猜你喜欢

转载自blog.csdn.net/qq_45654671/article/details/113004462