# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1054430 | matere | Password (RMI18_password) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// 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 <bits/stdc++.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;
}
string guess(int n, int s){
vector<pair<int,char> >amt;
for(char i='a';i<='a'+s-1;i++){
string q="";
for(int j=1;j<=n;j++){
q+=i;
}
amt.push_back({query(q),i});
}
sort(amt.begin(),amt.end());
// for(pair<int,char>i:amt){
// cout<<i.first<<' '<<i.second<<endl;
// }
string ans="";
for(int i=1;i<=n;i++) ans+=amt.back().second;
set<pair<int,char> >rem;
for(pair<int,char>i:amt) rem.insert(i);
for(pair<int,char>i:amt){
rem.erase(i);
for(int num=i.first;num>=1;num--){
string pr="";
for(int k=1;k<=num;k++) pr+=i.second;
int raod=0;
for(pair<int,char> j:rem){
string qu=pr;
for(int k=1;k<=j.first;k++){
qu+=j.second;
}
raod+=query(qu)-num;
}
int cur=n-1;
if(ans[cur]!=amt.back().second) raod++;
while(raod){
cur--;
raod--;
if(ans[cur]!=amt.back().second) raod++;
}
ans[cur]=i.second;
}
}
return ans;
}
int main() {
// ifstream f("password.in");
// f >> n >> s >> password;
// f.close();
cin >> n >> s >> password;
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;
}