From 91675b16a48a5b5058279178e53d1c36b2cf5e25 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 13 Jun 2022 10:42:06 +0800 Subject: [PATCH] D - Guess The String https://codeforces.com/contest/1697/submission/160389890 --- Codeforces/1697/D/D.cpp | 66 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Codeforces/1697/D/D.cpp diff --git a/Codeforces/1697/D/D.cpp b/Codeforces/1697/D/D.cpp new file mode 100644 index 00000000..4dca7771 --- /dev/null +++ b/Codeforces/1697/D/D.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +using std::endl; + +const int N = 1e4 + 5; + +int n, a[N], b[N], lst[N]; + +int query1(int x) { + char res; + cout << "? 1 " << x << endl; + cin >> res; + return res - 'a' + 1; +} + +int query2(int l, int r) { + int res; + cout << "? 2 " << l << ' ' << r << endl; + cin >> res; + return res; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + lst[a[1] = query1(1)] = 1; + + for (int i = 2; i <= n; i++) { + int cnt = 0; + for (int j = 1; j <= 26; j++) { + if (lst[j]) b[++cnt] = lst[j]; + } + + std::sort(b + 1, b + 1 + cnt); + + int l = 1, r = cnt, res = 0; + while (l <= r) { + int mid = l + r >> 1; + + if (query2(b[mid], i) == cnt - mid + 1) { + res = mid; + l = mid + 1; + } else { + r = mid - 1; + } + } + + lst[a[i] = res ? a[b[res]] : query1(i)] = i; + } + + cout << "! "; + for (int i = 1; i <= n; i++) { + cout << char(a[i] - 1 + 'a'); + } + cout << endl; + + return 0; +}