Submission #83812

#TimeUsernameProblemLanguageResultExecution timeMemory
83812AdrienVannsonCombo (IOI18_combo)C++17
94 / 100
88 ms928 KiB
#include <bits/stdc++.h>

#include "combo.h"

using namespace std;

const int NB_MAX_CARACTERES = 2000;

string res = "";
vector<char> possibles[NB_MAX_CARACTERES];


string guess_sequence (int nbCaracteres)
{
    srand(42);

    if (press("AB") > 0) {
        res += press("A") ? 'A' : 'B';
    }
    else {
        res += press("X") ? 'X' : 'Y';
    }

    if (nbCaracteres == 1) {
        return res;
    }

    possibles[0].push_back(res[0]);

    for (int iCaractere=1; iCaractere<nbCaracteres; iCaractere++) {
        for (char c : "ABXY") {
            if (c != res[0] && c != 0) {
                possibles[iCaractere].push_back(c);
            }
        }
    }

    while ((int)res.size() < nbCaracteres) {
        /*static int iter = 0;
        if (iter++ > 20) break;*/


        vector<string> mots;
        mots.push_back(res);

        while ((int)mots[0].size() < nbCaracteres) {
            const int iCaractere = mots[0].size();

            random_shuffle(possibles[iCaractere].begin(), possibles[iCaractere].end());


            vector<string> nouveauxMots;

            for (const string &mot : mots) {
                nouveauxMots.push_back(mot + possibles[iCaractere][0]);

                if (possibles[iCaractere].size() == 3) {
                    nouveauxMots.push_back(mot + possibles[iCaractere][1]);
                }
            }

            if ((int)nouveauxMots[0].size() * (int)nouveauxMots.size() <= 4*nbCaracteres) {
                mots = nouveauxMots;
            }
            else {
                break;
            }
        }

        /*cerr << "-----------------------" << endl;
        for (int i=0; i<nbCaracteres; i++) {
            cerr << "(";
            for (char p : possibles[i]) {
                cerr << p << " ";
            }
            cerr << ") ";
        }
        cerr << endl;*/

        string requete;
        for (string mot : mots) {
            requete += mot;
        }

        const int reponse = press(requete);
        //cerr << requete << " --> " << reponse << endl;

        // Caractères valides
        bool estValide = true;

        for (int iCaractere=res.size(); iCaractere<reponse; iCaractere++) {
            if (possibles[iCaractere].size() >= 2) {
                possibles[iCaractere].pop_back();
            }

            if (possibles[iCaractere].size() == 1 && estValide) {
                res += possibles[iCaractere][0];
            }
            else {
                estValide = false;
            }
        }

        // Dernier caractère, si il est invalide
        if (reponse < nbCaracteres && (int)mots[0].size() > reponse) {
            while (possibles[reponse].size() > 1) {
                possibles[reponse].erase(possibles[reponse].begin());
            }
        }
    }

    return res;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...