# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
857998 |
2023-10-07T09:23:50 Z |
Pety |
Password (RMI18_password) |
C++14 |
|
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>
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;
}*/
int fr[26];
string guess (int n, int s) {
for (char c = 'a'; c < 'a' + s; c++) {
string s;
for (int i = 0; i < n; i++)
s += c;
fr[c - 'a'] = query(s);
}
string a;
for (int i = 0; i < fr[0]; i++)
a += 'a';
for (int i = 1; i < s; i++) {
string b;
for (int j = 0; j < fr[i]; j++)
b += 'a' + i;
for (int j = 0; j < fr[i]; j++) {
int x = query(b + a);
a += 'a' + i;
x = x + j - (fr[i] - j);
int p = a.size() - 1;
while (x) {
swap(a[p], a[p - 1]);
p--;
x--;
}
b.pop_back();
}
}
return a;
}
/*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:63:21: warning: "/*" within comment [-Wcomment]
63 | if (i < plen) { /* found a match */
|
password.cpp:64:7: error: 'i' does not name a type
64 | i++;
| ^
password.cpp:65:7: error: 'j' does not name a type
65 | j++;
| ^
password.cpp:66:5: error: expected declaration before '}' token
66 | }
| ^
password.cpp:67:3: error: expected declaration before '}' token
67 | }
| ^
password.cpp:69:3: error: expected unqualified-id before 'if'
69 | if (DEBUG) {
| ^~
password.cpp:72:3: error: expected unqualified-id before 'return'
72 | return j;
| ^~~~~~
password.cpp:73:1: error: expected declaration before '}' token
73 | }*/
| ^
password.cpp:73:3: error: expected unqualified-id before '/' token
73 | }*/
| ^
password.cpp: In function 'std::string guess(int, int)':
password.cpp:82:5: error: 'fr' was not declared in this scope
82 | fr[c - 'a'] = query(s);
| ^~
password.cpp:82:19: error: 'query' was not declared in this scope
82 | fr[c - 'a'] = query(s);
| ^~~~~
password.cpp:85:23: error: 'fr' was not declared in this scope
85 | for (int i = 0; i < fr[0]; i++)
| ^~
password.cpp:89:25: error: 'fr' was not declared in this scope
89 | for (int j = 0; j < fr[i]; j++)
| ^~
password.cpp:91:25: error: 'fr' was not declared in this scope
91 | for (int j = 0; j < fr[i]; j++) {
| ^~
password.cpp:92:15: error: 'query' was not declared in this scope
92 | int x = query(b + a);
| ^~~~~