题解 CF1292E 【Rin and The Unknown Flower】

· · 题解

CF1292E Rin and The Unknown Flower

题目大意

题目链接

一个长度为 n 的字符串 S,只包含 \texttt{C},\texttt{H},\texttt{O} 三个字母,你每次可以询问一个字符串 p,交互库会告诉你 p 作为 S 的子串出现的开头位置是哪些。

假设 p 的长度为 t,那么这次询问的代价为 \frac{1}{t^2} 。你需要在总代价不超过 1.4 内询问出字符串 S。

数据范围:4\leq n\leq 50。

本题题解

暴力做法:花费 2 问 \texttt{C},\texttt{O},剩下的位置就是 \texttt{H}。

这种做法给了我们一些启发,事实上可以稍微加长一下询问的串长。

我们询问 \texttt{CC}, \texttt{CH}, \texttt{CO}。这样就能以 3\times \frac{1}{2^2} = 0.75 的代价,知道(除最后一个位置外)所有 \texttt{C} 出现的位置。

同理,我们还想知道所有 \texttt{O} 出现的位置。不过考虑到前面已经问过一次 \texttt{CO},所以只需要再来两次询问 \texttt{HO}, \texttt{OO},就能知道(除第一个位置外)所有 \texttt{O} 出现的位置。

至此一共使用了 5 次询问(\texttt{CC}, \texttt{CH}, \texttt{CO}, \texttt{HO}, \texttt{OO}),代价为 5\times \frac{1}{2^2} = 1.25。串里所有还不确定的位置,除第一个和最后一个位置外,它的字符一定是 \texttt{H}。于是我们已知了除第一个和最后一个位置外的所有字符。

接下来处理第一个和最后一个位置(如果它们仍然未知的话)。第一个位置只可能是 \texttt{O},\texttt{H},最后一个位置只可能是 \texttt{C}, \texttt{H}。共有 2\times 2 = 4 种可能。我们只需要进行 3 次长度为 n 的询问(如果都不成功,则必然是最后一种,不需要问了)。

至此花费的总代价为 1.25 + \frac{3}{n^2}。在 n > 4 时可以成功。

接下来单独处理 n = 4 的情况。

仍然先询问 \texttt{CC}, \texttt{CH}, \texttt{CO}。如果至少有一次出现,那么至多只剩两位不确定。且前 3 位(如果还不确定的话)不可能是 \texttt{C},所以至多只有 2\times 3 = 6 种可能,只需要 5 次询问。花费的总代价是:3\times \frac{1}{2^2} + 5\times \frac{1}{4^2} = 1.0625。

如果 \texttt{CC}, \texttt{CH}, \texttt{CO} 都没有出现过,再询问 \texttt{HO}。如果它出现过,可以用类似的方法,以 4\times \frac{1}{2^2} + 5\times \frac{1}{4^2} = 1.3125 的代价求出答案。

如果 \texttt{CC}, \texttt{CH}, \texttt{CO}, \texttt{HO} 都没有出现过,再问 \texttt{OO}。如果 \texttt{OO} 出现过,那么目前已知的串,可能有如下三种情形:

注意,不可能出现 \texttt{**OO} 或 \texttt{*OOO} 的情况。因为此时两个 \texttt{O} 前面,不可能是 \texttt{C}(否则在 \texttt{CO} 就被问出来了),不可能是 \texttt{H}(否则在 \texttt{HO} 就被问出来了),也不可能是 \texttt{O}(否则在 \texttt{OO} 就被问出来了)。

综上所述,最多只有 1 个位还不确定,且它最多只有 2 种情况。所以只需要再来 1 次询问。总代价是:5\times\frac{1}{2^2} + 1\times\frac{1}{4^2} = 1.3125。

最后,如果 \texttt{CC}, \texttt{CH}, \texttt{CO}, \texttt{HO}, \texttt{OO} 都没有出现过,那么中间两位一定是 \texttt{HH}。此时询问 \texttt{HHH}。第一位如果是 \texttt{H},那么它会被问出来,否则它一定是 \texttt{O}。同理,最后一位也可以确定。总代价是:5\times \frac{1}{2^2} + 1\times\frac{1}{3^2} = 1.3611。

于是就做完了。实现的时候,可以把每个位置可能的选项,用一个 vector 存起来,然后 \text{dfs} 一遍。这样可以避免复杂的讨论。详见参考代码。

参考代码

// problem: CF1292E
#include <bits/stdc++.h>
using namespace std;

#define mk make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

template<typename T> inline void ckmax(T& x, T y) { x = (y > x ? y : x); }
template<typename T> inline void ckmin(T& x, T y) { x = (y < x ? y : x); }

const int MAXN = 50;

int n;
char ans[MAXN + 5];

inline void fix(int i, char c) {
    assert(ans[i] == 0 || ans[i] == c);
    ans[i] = c;
}
vector<int> ask(string s) {
    cout << "? " << s << endl;
    vector<int> res;

    int k;
    cin >> k;

    if (k == -1) exit(0); // WA

    res.resize(k);
    for (int i = 0; i < k; ++i) {
        cin >> res[i];
        assert(res[i] + SZ(s) - 1 <= n);
        for (int j = 0; j < SZ(s); ++j) {
            fix(res[i] + j, s[j]);
        }
    }
    return res;
}

void report_ans() {
    cout << "! ";
    for (int i = 1; i <= n; ++i) cout << ans[i];
    cout << endl;

    int t; cin >> t;
    if (t == 0) exit(0); // WA
}


vector<char> ch[MAXN + 5];

bool dfs(int idx, string s, bool flag) {

    if (idx == n + 1) {
        if (flag) {
            // 每一位都是最后一个选项
            // 那么不用问了, 一定是答案

            report_ans();
            return true;
        }
        if (SZ(ask(s))) {
            report_ans();
            return true;
        }

        return false;
    }

    for (int i = 0; i < SZ(ch[idx]); ++i) {
        ans[idx] = ch[idx][i];
        bool t = dfs(idx + 1, s + ch[idx][i], flag && (i == SZ(ch[idx]) - 1));
        if (t) return true;
    }
    return false;
}

void solve_case() {
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        ans[i] = 0;
        vector<char>().swap(ch[i]); // clear
    }

    if (n > 4) {

        ask("CC");
        ask("CH");
        ask("CO");
        ask("HO");
        ask("OO");

        for (int i = 2; i < n; ++i) {
            if (ans[i] == 0) {
                ans[i] = 'H';
            }
        }

        for (int i = 1; i <= n; ++i) {
            if (ans[i] != 0) {
                ch[i].push_back(ans[i]);
            } else if (i == 1) {
                // ans[1] = 'H' or 'O'
                ch[i].push_back('H');
                ch[i].push_back('O');
            } else {
                // i == n
                // ans[n] = 'C' or 'H'
                ch[i].push_back('C');
                ch[i].push_back('H');
            }
        }

        dfs(1, "", 1);
    } else {
        vector<int> tmp;
        bool flag = 0;
        tmp = ask("CC"); if (SZ(tmp)) flag = 1;
        tmp = ask("CH"); if (SZ(tmp)) flag = 1;
        tmp = ask("CO"); if (SZ(tmp)) flag = 1;
        tmp = ask("HO"); if (SZ(tmp)) flag = 1;

        if (flag) {
            for (int i = 1; i <= n; ++i) {
                if (ans[i] != 0) {
                    ch[i].push_back(ans[i]);
                } else if (i <= 3) {
                    // ans[i] = 'H' or 'O'
                    ch[i].push_back('H');
                    ch[i].push_back('O');
                } else {
                    ch[i].push_back('C');
                    ch[i].push_back('H');
                    ch[i].push_back('O');
                }
            }

            dfs(1, "", 1);

            return;
        }

        tmp = ask("OO");

        if (SZ(tmp)) {
            if (ans[1] == 'O' && ans[2] == 'O' && ans[3] == 'O' && ans[4] == 'O') {
                // OOOO
                report_ans();
                return;
            } else if (ans[1] == 'O' && ans[2] == 'O' && ans[3] == 'O') {
                // OOO*
                ch[1].push_back('O');
                ch[2].push_back('O');
                ch[3].push_back('O');

                assert(ans[4] == 0);
                ch[4].push_back('C');
                ch[4].push_back('H');

                dfs(1, "", 1);
                return;
            } else if (ans[1] == 'O' && ans[2] == 'O') {
                // OOH*
                ans[3] = 'H';
                ch[1].push_back('O');
                ch[2].push_back('O');
                ch[3].push_back('H');

                assert(ans[4] == 0);
                ch[4].push_back('C');
                ch[4].push_back('H');

                dfs(1, "", 1);
                return;
            }

            assert(0);
        }

        // ans[2] 和 ans[3] 一定是 'H'
        ans[2] = ans[3] = 'H';

        ask("HHH");

        if (!ans[1]) {
            ans[1] = 'O';
        }
        if (!ans[4]) {
            ans[4] = 'C';
        }

        report_ans();
        return;
    }
}
int main() {
    int T; cin >> T; while (T--) {
        solve_case();
    }
    return 0;
}