题解:AT_arc177_b [ARC177B] Puzzle of Lamps

· · 题解

题目大意

有一个一开始全为 0 的序列,有如下两种操作:

给你一个序列,构造任意一种方案进行若干次操作后得到这个序列。

题目思路

通过瞪眼大法,我们可以发现,这个家伙与前缀和有那么一丝丝的相似。

从后往前扫,每一次找到第一个不连续的 1 和 0,按下标分别输出 A 和 B 即可。

AC Code

#include <cstring>
#include <iostream>
#include <algorithm>

#define endl '\n'
#define int long long
#define inf 0x3f3f3f3f
#define lnf 0x3f3f3f3f3f3f3f3f

//#prgama GCC optimize(1)
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize("Os")
//#pragma GCC optimize("Ofast")

using namespace std;

inline bool input (int x) {
    x = 0;
    int f = 1;
    bool flag = 0;
    char c = getchar ();

    while (!isdigit(c)) {
        if (c == '-') {
            c = getchar ();

            if (isdigit (c)) {
                f = -1;
                break;
            }
        } else {
            c = getchar ();
        }
    }

    while (isdigit (c)) {
        flag = 1;
        x = (x << 3) + (x << 1) + (c ^ 48);
        c = getchar ();
    }

    x *= f;
    return flag;
}

inline bool output (int x) {
    if (x < 0) {
        putchar ('-');
        x = -x;
    }

    char write[25], cnt = 0;

    do {
        write[++ cnt] = x % 10;
        x /= 10;
    } while (x);

    do {
        putchar (write[cnt --] | 48);
    } while (cnt);

    return x;
}

const int N = 30;

void init ();

int n;
char a[N + 5];
bool flag;
string ans;

signed main () {
    init ();
    input (n);
    cin >> a + 1;

    for (int i = n; i; -- i) {
        if (a[i] == '1' && !flag) {
            flag = 1;

            for (int j = 1; j <= i; ++ j) {
                ans += "A";
            }
        }

        if (a[i] == '0' &&  flag) {
            flag = 0;

            for (int j = 1; j <= i; ++ j) {
                ans += "B";
            }
        }
    }

    output (ans.size ());
    puts ("");
    cout << ans;
    return 0;
}

void init () {
//  freopen (".in", "r", stdin);
//  freopen (".out", "w", stdout);
//  ios::sync_with_stdio (false);
//  cin.tie (0);
//  cout.tie (0);
}