답안 #722062

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
722062 2023-04-11T11:27:37 Z nguyentunglam Password (RMI18_password) C++17
컴파일 오류
0 ms 0 KB
// Sample grader for contestants' use.
//
// Usage: place your input data in the file password.in in the format
//
// line 1: N S
// line 2: hidden_password
//
// Then compile this grader together with your implementation.
// Run the binary to see your guessing strategy at work!
// Set DEBUG to 1 to print all queries.
#include<bits/stdc++.h>
using namespace std;
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <assert.h>

using namespace std;

#define DEBUG 0
#define MAX_QUERIES 50000
#define MAX_N 5000
#define MAX_SIGMA 26

static string password;
static int n, s, numQueries;
static stringstream msg;

static void end(int success) {
  cout << (success ? "SUCCESS! " : "ERROR: ") << msg.str() << endl;
  exit(0);
}

int query(string q) {
  if (++numQueries > MAX_QUERIES) {
    msg << "Could not guess the password with " << MAX_QUERIES << " questions.";
    end(0);
  }

  int len = q.size();

  // validation
  if (len < 1 || len > n) {
    msg << "Query '" << q << "' should have length between 1 and "
        << n << " characters.";
    end(0);
  }

  for (int i = 0; i < len; i++) {
    if (q[i] < 'a' || q[i] >= 'a' + s) {
      msg << "Illegal character '" << q[i] << "' found.";
      end(0);
    }
  }

  // while possible, advance one character in q and find its next
  // occurrence in password
  int i = 0, j = 0, plen = password.size();
  while (i < plen && j < len) {
    while ((i < plen) && (password[i] != q[j])) {
      i++;
    }
    if (i < plen) { /* found a match */
      i++;
      j++;
    }
  }

  if (DEBUG) {
    cout << "Question #" << numQueries << " [" << q << "] answer " << j << endl;
  }
  return j;
}

const int N = 5010;
int cnt[N];
char a[N], b[N];
string guess(int n, int s) {
    vector<pair<int, char> > lst;
    for(int j = 0; j < s; j++) {
        char c = j + 'a';
        string ask;
        for(int i = 1; i <= n; i++) ask.push_back(c);
        lst.emplace_back(query(ask), c);
    }
    sort(lst.begin(), lst.end());

    int m; char c;
    tie(m, c) = lst[0];
    for(int i = 1; i <= m; i++) a[i] = c;
    lst.erase(lst.begin());

    for(auto &it : lst) {
        int num; char c;
        tie(num, c) = it;
        for(int i = 0; i <= m; i++) {
            string ask;
            for(int j = 1; j <= i; j++) ask.push_back(a[j]);
            cnt[i] = 0;
            while (num > 0) {
                ask.push_back(c);
                string tmp;
                for(int j = i + 1; j <= m; j++) tmp.push_back(a[j]);
                if (query(ask + tmp) > m + cnt[i]) cnt[i]++, num--;
                else break;
            }
        }
        int m2 = 0;
        for(int i = 0; i <= m; i++) {
            if (i) b[++m2] = a[i];
            while (cnt[i]--) b[++m2] = c;
        }
        for(int i = 1; i <= m2; i++) a[i] = b[i];
        m = m2;
    }
    string ret;
    for(int i = 1; i <= m; i++) ret.push_back(a[i]);
    return ret;
}

int main() {

//  ifstream f("password.in");
  cin >> n >> s >> password;
//  cin.close();
  assert(n && s && !password.empty());

  string answer = guess(n, s);
  if (DEBUG) {
    cout << "Your answer: [" << answer << "]\n";
  }

  if (answer.compare(password)) {
    msg << "Your answer was [" << answer
        << "] which does not match the password [" << password << "].";
    end(0);
  } else {
    msg << "You guessed the password with " << numQueries << " queries.";
    end(1);
  }

	return 0;
}

Compilation message

/usr/bin/ld: /tmp/ccK5DMJU.o: in function `query(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
grader.cpp:(.text+0x80): multiple definition of `query(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'; /tmp/ccTAaTiS.o:password.cpp:(.text+0x690): first defined here
/usr/bin/ld: /tmp/ccK5DMJU.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccTAaTiS.o:password.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status