Codeforces Round #712 (Div. 2) 个人题解

这一场打的又很差(掉分预定),D题想不出来。

A. Déjà Vu

这题首先判断字符串是否全由 a 组成,如果是的话输出 NO

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0);
    int _;
    for (cin >> _; _--;) {
        string s;
        cin >> s;
        int N = s.length();
        // find_first_not_of
         auto i = s.find_first_not_of('a');
        if (i == string::npos) {
            cout << "NO\n";
            continue;
        }
        int j = (i < N / 2 ? N - i : N - i - 1);
        cout << "YES\n";
        cout << s.substr(0, j) << 'a' << s.substr(j) << "\n";
    }
    return 0;
}

B. Flip the Bits

题意:给你一个 01 初始序列和目标序列,每次可以选择前 \(2\times x\) 个位置异或1(前提是选择区域的0和1个数相等),问你有没有办法变成目标序列。

思路:待补

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0);
    int _;
    for (cin >> _; _--;) {
        int n;
        string a, b;
        cin >> n >> a >> b;
        int sa = 0, sb = 0;
        bool f = true;
        for (int i = 0; i < n; ++i) {
            sa += 2 * (a[i] - '0') - 1;
            sb += 2 * (b[i] - '0') - 1;
            // cout << sa << " " << sb << "\n";
            f = f && abs(sa) == abs(sb);
        }
        // cout << sa << " " << sb << "\n";
        f = f && sa == sb;
        cout << (f ? "YES\n" : "NO\n");
    }
    return 0;
}

C. Balance the Bits

题意:给你一个01序列,问你能不能构造两个合法的括号序列a,b,使得当 \(s[i] = 1\) 时,\(a[i] = b[i]\) ,当 \(s[i] = 0\) 时,\(a[i] != b[i]\)

思路:首先,两个字符串序列必须以( 开头,) 结尾,其实,由于 ()的个数和 \(n\) 为偶数所以 \(s\) 序列中 1 的个数也必为偶数。

接下来就是模拟条件了

AC 代码

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0);
    int _;
    for (cin >> _; _--;) {
        int n, t = 0;
        string s;
        cin >> n >> s;
        int cnt = count(s.begin(), s.end(), '1');
        if (s[0] != '1' || s[n - 1] != '1' || cnt & 1) {
            cout << "NO\n";
            continue;
        }
        cnt /= 2;
        string a(n, '*'), b(n, '*');
        for (int i = 0; i < n; ++i) {
            if (s[i] == '0') {
                if (t == 0) a[i] = ')', b[i] = '(';
                else
                    a[i] = '(', b[i] = ')';
                t ^= 1;
            } else {
                if (cnt) {
                    a[i] = b[i] = '(';
                    cnt--;
                } else
                    a[i] = b[i] = ')';
            }
        }
        cout << "YES\n";
        cout << a << "\n"
             << b << '\n';
    }
    return 0;
}
posted @ 2021-04-04 20:16  Koshkaaa  阅读(72)  评论(0编辑  收藏  举报