P10244 题解

· · 题解

贪心题。首先为了保证 a 的字典序小,当 a_i<c_i 时要交换,a_i>c_i 时不交换。在 a_i=c_i 时,为了使 b 的字典序尽可能小,则当 b_i>d_i 时不交换,否则交换。

代码

# include <bits/stdc++.h>
# define ffor(i,name) \
    for (auto i = name.begin (); i != name.end (); ++ i)
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int n;
string a, b, c, d;
int main () {
    ios::sync_with_stdio (0);
    cin.tie (0);
    cout.tie (0);
    cin >> n >> a >> b >> c >> d;
    for (int i = 0; i < n; ++ i)
        if (a[i] > c[i])
            b[i] = d[i];
        else if (a[i] >= c[i])
            b[i] = min (b[i], d[i]);
    cout << b;
    return 0;
}