제출 #261069

#제출 시각아이디문제언어결과실행 시간메모리
261069handlenamePassword (RMI18_password)C++17
컴파일 에러
0 ms0 KiB
#include <bits/stdc++.h>
//#include "pandagames.h"
using namespace std;
#define pb push_back
#define mp make_pair
// 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.

#define DEBUG 1
#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;
}

int num[26];
string guess(int n,int s){
    set<pair<int,string> > process;
    for (int i=0;i<s;i++){
        string cur;
        for (int j=0;j<n;j++) cur.pb('a'+i);
        num[i]=query(cur);
        cur.clear();
        for (int j=0;j<num[i];j++){
            cur.pb('a'+i);
        }
        process.insert(mp(num[i],cur));
    }
    while (process.size()>1){
        string one=(*(process.begin())).second;
        process.erase(process.begin());
        string two=(*(process.begin())).second;
        process.erase(process.begin());
        string res=two;
        int id=0; //idth guy not fixed
        for (int i=0;i<one.size();i++){
            string cur=res;
            while (true){
                string lol;
                for (int j=0;j<id;j++){
                    lol.pb(cur[j]);
                }
                lol.pb(one[i]);
                for (int j=id;j<cur.size();j++){
                    lol.pb(cur[j]);
                }
                if (query(lol)==lol.size()){
                    cur=lol;
                    break;
                }
                id++;
            }
            id++;
            res=cur;
        }
        process.insert(mp(res.size(),res));
    }
    return (*(process.begin())).second;
}

int main() {

  fstream f("password.in");
  f >> n >> s >> password;
  f.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;
}

컴파일 시 표준 에러 (stderr) 메시지

password.cpp: In function 'std::__cxx11::string guess(int, int)':
password.cpp:92:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i=0;i<one.size();i++){
                      ~^~~~~~~~~~~
password.cpp:100:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 for (int j=id;j<cur.size();j++){
                               ~^~~~~~~~~~~
password.cpp:103:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 if (query(lol)==lol.size()){
                     ~~~~~~~~~~^~~~~~~~~~~~
/tmp/ccvjzNCb.o: In function `query(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
grader.cpp:(.text+0x0): multiple definition of `query(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/cc2JCj6W.o:password.cpp:(.text+0x1d0): first defined here
/tmp/ccvjzNCb.o: In function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/cc2JCj6W.o:password.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status