TIOJ 1262:[Interactive] 猜句子

TIOJ 1262[Interactive] 猜句子


題目大意:題目會有一個秘密字串,每次可以詢問一個字串是否為秘密字串的子字串,目標是要猜到這個字串,但是詢問有 $\frac{1}{32768}$ 的機率會出錯。

解法:往後面加字元,再往前面加字元,如果不能再加了,就停止。然後我用了兩次 $\text{Guess}$ 防止出錯。

$\text{Code:}$

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include "lib1262.h"
using namespace std;

#define Waimai ios::sync_with_stdio(false),cin.tie(0)
#define FOR(x,a,b) for(int x=a;x<=b;x++)
#define pb emplace_back
#define F first
#define S second

bool guess (string s) {
    int cnt = Guess (s.c_str(), s.size()) + Guess (s.c_str(), s.size());
    if (cnt & 1) {
        return Guess (s.c_str(), s.size());
    }
    return !!cnt;
}

void solve() {
    string s;
    while (1) {
        bool ok = 0;
        for (char c = 'a'; c <= 'z'; c++) {
            if (guess (s + c)) {
                ok = 1;
                s += c;
                break;
            }
        }
        if (!ok) {
            break;
        }
    }
    while (1) {
        bool ok = 0;
        for (char c = 'a'; c <= 'z'; c++) {
            if (guess (c + s)) {
                ok = 1;
                s = c + s;
                break;
            }
        }
        if (!ok) {
            break;
        }
    }
    cout << s << '\n';
}

int main() {
    Initialize();
    solve();
}

我的分享就到這裡結束了,如果喜歡我的 $\text{Blog}$,歡迎追蹤!

留言

熱門文章