This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int query(string str);
///editorial solution, based on quicksort?, s+n+nlogs supposedly
char ans[5005];
int N;
int tot_occ[128];
string repeat(char c, int n){
string ret = "";
for (int x = 0; x<n; x++){
ret+=c;
}
return ret;
}
void rec(int l,int r,vector<pair<char,int> > chars){
///assume all subproblems to the left have been solved
///base case
if (chars.size()==0) return;
if (chars.size()==1){
for (int x = l; x<l+chars[0].second; x++){
ans[x] = chars[0].first;
}
return;
}
///randomly choose pivot
int pos = rand()%(r-l+1);
int occ = 0; ///occ-th occurrence of the character (0-index)
char ch;
int totalch;
for (auto x : chars){
if (x.second>pos){
occ = pos;
ch = x.first;
totalch = x.second;
break;
}
pos -= x.second;
}
int occbef = 0; ///occurrences to the left of the subproblem
for (int x = 0; x<l; x++){
if (ans[x]==ch){
occbef++;
}
}
vector<pair<char,int> > lch,rch;
for (auto x : chars){
if (x.first==ch) continue;
string qu = repeat(ch,occbef+occ+1);
qu += repeat(x.first,N-qu.size());
int res = query(qu);
res -= occbef+occ+1;
res = tot_occ[x.first]-res;
for (int y = 0; y<l; y++){
if (ans[y]==x.first) res--;
}
if (res>0){
lch.push_back({x.first,res});
}
if (x.second-res>0){
rch.push_back({x.first,x.second-res});
}
}
if (occ>0){
lch.push_back({ch,occ});
}
if (totalch-occ-1>0){
rch.push_back({ch,totalch-occ-1});
}
int offset = 0;
for (auto x : lch){
offset += x.second;
}
rec(l,l+offset-1,lch);
ans[l+offset] = ch;
rec(l+offset+1,r,rch);
return;
}
string guess(int n, int s){
vector<pair<char,int> > chars;
N = n;
for (int x = 0; x<s; x++){
string qu = repeat('a'+x,n);
int t = query(qu);
if (t>0){
chars.push_back({'a'+x,t});
}
tot_occ['a'+x] = t;
}
rec(0,n-1,chars);
string Ans = "";
for (int x = 0; x<n; x++){
Ans += ans[x];
}
return Ans;
}
Compilation message (stderr)
password.cpp: In function 'void rec(int, int, std::vector<std::pair<char, int> >)':
password.cpp:61:30: warning: array subscript has type 'char' [-Wchar-subscripts]
res = tot_occ[x.first]-res;
^
password.cpp:75:5: warning: 'totalch' may be used uninitialized in this function [-Wmaybe-uninitialized]
if (totalch-occ-1>0){
^~
password.cpp:57:43: warning: 'ch' may be used uninitialized in this function [-Wmaybe-uninitialized]
string qu = repeat(ch,occbef+occ+1);
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |