Submission #1064701

# Submission time Handle Problem Language Result Execution time Memory
1064701 2024-08-18T17:04:08 Z AlgorithmWarrior Password (RMI18_password) C++14
Compilation error
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 <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <assert.h>
#include <queue>

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;
}

class cmp
{
public:
    bool operator()(string a,string b)
    {
        return a.size()>b.size();
    }
};

string guess(int n, int s)
{
    int cnt[300];
    char ch;
    priority_queue<string,vector<string>,cmp>pq;
    for(ch='a';ch<'a'+s;++ch)
    {
        int i;
        string s="";
        for(i=1,s=ch;query(s)==s.size();++i,s+=ch);
        cnt[ch]=i-1;
        s.pop_back();
        if(s.size())
            pq.push(s);
    }
    while(pq.size()>1)
    {
        string s1=pq.top(); pq.pop();
        string s2=pq.top(); pq.pop();
        int i,j=0;
        int siz=s1.size();
        int siz2=s2.size();
        string rez="";
        string* p=&s1;
        for(i=0;i<=siz;++i)
        {
            while(j<siz2 && query(rez+s2[j]+*p)==(rez+s2[j]+*p).size())
                rez+=s2[j++];
            if(i<siz)
            {
                rez+=s1[i];
                ++p;
            }
        }
        pq.push(rez);
    }
    return pq.top();
}

int main() {

  ifstream 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;
}

Compilation message

password.cpp: In function 'std::string guess(int, int)':
password.cpp:94:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |         for(i=1,s=ch;query(s)==s.size();++i,s+=ch);
      |                      ~~~~~~~~^~~~~~~~~~
password.cpp:95:13: warning: array subscript has type 'char' [-Wchar-subscripts]
   95 |         cnt[ch]=i-1;
      |             ^~
password.cpp:111:48: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |             while(j<siz2 && query(rez+s2[j]+*p)==(rez+s2[j]+*p).size())
      |                             ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
password.cpp:87:9: warning: variable 'cnt' set but not used [-Wunused-but-set-variable]
   87 |     int cnt[300];
      |         ^~~
/usr/bin/ld: /tmp/ccKS1EFh.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/cc2fdIMh.o:password.cpp:(.text+0x560): first defined here
/usr/bin/ld: /tmp/ccKS1EFh.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/cc2fdIMh.o:password.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status