Problem: cf1286_c2
Difficulty: medium
Categories: graph, data structures, math
Time limit: 1000 ms, memory limit: 256 MB

This problem is different with easy version only by constraints on total answers length

It is an interactive problem

Venya joined a tour to the madhouse, in which orderlies play with patients the following game. Orderlies pick a string $s$ of length $n$, consisting only of lowercase English letters. The player can ask two types of queries:

* ? l r – ask to list all substrings of $s[l..r]$. Substrings will be returned in random order, and in every substring, all characters will be randomly shuffled.
* ! s – guess the string picked by the orderlies. This query can be asked exactly once, after that the game will finish. If the string is guessed correctly, the player wins, otherwise he loses.

The player can ask no more than $3$ queries of the first type.

To make it easier for the orderlies, there is an additional limitation: the total number of returned substrings in all queries of the first type must not exceed $\left\lceil 0.777(n+1)^2 \right\rceil$ ($\lceil x \rceil$ is $x$ rounded up).

Venya asked you to write a program, which will guess the string by interacting with the orderlies' program and acting by the game's rules.

Your program should immediately terminate after guessing the string using a query of the second type. In case your program guessed the string incorrectly, or it violated the game rules, it will receive verdict Wrong answer.

Note that in every test case the string is fixed beforehand and will not change during the game, which means that the interactor is not adaptive.

Input

First line contains number $n$ ($1 \le n \le 100$) — the length of the picked string.

Interaction

You start the interaction by reading the number $n$.

To ask a query about a substring from $l$ to $r$ inclusively ($1 \le l \le r \le n$), you should output

? l r

on a separate line. After this, all substrings of $s[l..r]$ will be returned in random order, each substring exactly once. In every returned substring all characters will be randomly shuffled.

In the case, if you ask an incorrect query, ask more than $3$ queries of the first type or there will be more than $\left\lceil 0.777(n+1)^2 \right\rceil$ substrings returned in total, you will receive verdict Wrong answer.

To guess the string $s$, you should output

! s

on a separate line.

After printing each query, do not forget to flush the output. Otherwise, you will get Idleness limit exceeded. To flush the output, you can use:

* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.

If you received - (dash) as an answer to any query, you need to terminate your program with exit code 0 (for example, by calling exit(0)). This means that there was an error in the interaction protocol. If you don't terminate with exit code 0, you can receive any unsuccessful verdict.

Example

Input

```
4

a
aa
a

cb
b
c

c
```

Output

```
? 1 2

? 3 4

? 4 4

! aabc
```

Non-adaptive interactor (C++):
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

static long long queries = 0;
static long long query_limit = 0;

static void log_metrics() {
    try {
        tout << "queries=" << queries << endl;
        tout << "query_limit=" << query_limit << endl;
    } catch (...) {
        
    }
}

static void finish(TResult verdict, const string& msg) {
    log_metrics();
    quitf(verdict, "%s", msg.c_str());
}

static bool parseNonNegIntToken(const string& tok, long long& out) {
    if (tok.empty()) return false;
    long long val = 0;
    for (char c : tok) {
        if (c < '0' || c > '9') return false;
        int d = c - '0';
        if (val > (LLONG_MAX - d) / 10) return false;
        val = val * 10 + d;
    }
    out = val;
    return true;
}

static long long ceil_777_thousand(long long x) {
    
    __int128 num = (__int128)777 * (__int128)x;
    return (long long)((num + 999) / 1000);
}

int main(int argc, char** argv) {
    registerInteraction(argc, argv);
    atexit(log_metrics);

    int n = inf.readInt(1, 100, "n");
    string hidden = inf.readToken();
    if ((int)hidden.size() != n) finish(_fail, "Hidden string length mismatch with n");
    for (char c : hidden) if (c < 'a' || c > 'z') finish(_fail, "Hidden string contains non-lowercase letters");

    query_limit = 3;

    
    log_metrics();

    cout << n << endl;

    long long hard_query_cap = max(200000LL, 10LL * query_limit);
    long long official_total_substrings_limit = ceil_777_thousand(1LL * (n + 1) * (n + 1));
    long long hard_substrings_cap = max(200000LL, 10LL * official_total_substrings_limit);

    long long total_returned_substrings = 0;

    string cmd;
    while (true) {
        if (!(cin >> cmd)) {
            finish(_pe, "Unexpected EOF from contestant");
        }

        if (cmd == "?") {
            string tokL, tokR;
            if (!(cin >> tokL >> tokR)) {
                cout << "-" << endl;
                finish(_pe, "Malformed query: expected '? l r'");
            }

            long long l = -1, r = -1;
            if (!parseNonNegIntToken(tokL, l) || !parseNonNegIntToken(tokR, r)) {
                cout << "-" << endl;
                finish(_wa, "Invalid query indices");
            }

            if (!(1 <= l && l <= r && r <= n)) {
                cout << "-" << endl;
                finish(_wa, "Query indices out of range");
            }

            queries++;
            if (queries > hard_query_cap) {
                cout << "-" << endl;
                finish(_pe, "Hard cap exceeded (too many queries)");
            }

            int L = (int)l - 1;
            int R = (int)r - 1;
            int m = R - L + 1;
            long long k = 1LL * m * (m + 1) / 2;

            total_returned_substrings += k;
            if (total_returned_substrings > hard_substrings_cap) {
                cout << "-" << endl;
                finish(_pe, "Hard cap exceeded (too many substrings requested)");
            }

            
            
            for (int i = 0; i < m; i++) {
                string cur;
                cur.reserve(m - i);
                for (int j = i; j < m; j++) {
                    cur.push_back(hidden[L + j]);
                    string outStr = cur;
                    sort(outStr.begin(), outStr.end()); 
                    cout << outStr << "\n";
                }
            }
            cout.flush();
            continue;
        }

        if (cmd == "!") {
            string guess;
            if (!(cin >> guess)) {
                finish(_pe, "Malformed answer: expected '! s'");
            }
            if ((int)guess.size() != n) {
                finish(_wa, "Wrong answer (length mismatch)");
            }
            for (char c : guess) {
                if (c < 'a' || c > 'z') finish(_wa, "Wrong answer (non-lowercase letters)");
            }
            if (guess == hidden) finish(_ok, "OK");
            finish(_wa, "Wrong answer");
        }

        cout << "-" << endl;
        finish(_pe, "Unknown command (expected '?' or '!')");
    }
}


Adaptive interactor (C++):

Test generator (C++):
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

static int clampInt(int x, int lo, int hi) {
    if (x < lo) return lo;
    if (x > hi) return hi;
    return x;
}

static string makeAlternating(int n, int k) {
    string s;
    s.reserve(n);
    int a = 0;
    int b = (k >= 2 ? 1 : 0);
    for (int i = 0; i < n; i++) s.push_back(char('a' + (i % 2 ? b : a)));
    return s;
}

static string makeBlocks(int n, int k) {
    string s;
    s.reserve(n);
    int i = 0;
    int prev = -1;
    while (i < n) {
        int c = rnd.next(0, k - 1);
        if (k >= 2 && c == prev) c = (c + 1) % k;
        int maxLen = min(n - i, 1 + rnd.next(1, 12));
        int len = rnd.next(1, maxLen);
        for (int j = 0; j < len; j++) s.push_back(char('a' + c));
        i += len;
        prev = c;
    }
    return s;
}

static string makePalindromeWithNoise(int n, int k) {
    string s(n, 'a');
    int half = (n + 1) / 2;
    for (int i = 0; i < half; i++) {
        int c = rnd.next(0, k - 1);
        s[i] = char('a' + c);
        s[n - 1 - i] = char('a' + c);
    }
    int flips = max(1, n / 12);
    for (int t = 0; t < flips; t++) {
        int pos = rnd.next(0, n - 1);
        int c = rnd.next(0, k - 1);
        s[pos] = char('a' + c);
    }
    return s;
}

static string makeBiasedRandom(int n, int k) {
    vector<int> weights(k, 1);
    if (k >= 1) weights[0] = 8;
    if (k >= 2) weights[1] = 6;
    if (k >= 3) weights[2] = 4;
    if (k >= 4) weights[3] = 3;

    long long sum = 0;
    for (int w : weights) sum += w;

    string s;
    s.reserve(n);
    for (int i = 0; i < n; i++) {
        long long x = rnd.next(1LL, sum);
        long long acc = 0;
        int pick = 0;
        for (int j = 0; j < k; j++) {
            acc += weights[j];
            if (x <= acc) {
                pick = j;
                break;
            }
        }
        s.push_back(char('a' + pick));
    }

    if (n >= 4) {
        s[0] = 'a';
        s[n - 1] = char('a' + min(k - 1, 1));
        s[1] = char('a' + min(k - 1, 2));
        s[n - 2] = char('a' + min(k - 1, 3));
    }
    return s;
}

static string makeAdversarialMix(int n, int k) {
    int t = rnd.next(0, 3);
    string s;
    if (t == 0) s = makeBlocks(n, k);
    else if (t == 1) s = makePalindromeWithNoise(n, k);
    else if (t == 2) s = makeAlternating(n, k);
    else s = makeBiasedRandom(n, k);

    if (n >= 10 && k >= 2) {
        int p1 = n / 3;
        int p2 = (2 * n) / 3;
        s[p1] = 'a';
        s[p2] = 'b';
    }
    if (n >= 20 && k >= 4) {
        s[n / 2] = 'c';
        s[min(n - 1, n / 2 + 1)] = 'd';
    }
    return s;
}

int main(int argc, char** argv) {
    registerGen(argc, argv, 1);

    string mode = opt<string>("mode", "non");
    int forcedN = opt<int>("n", -1);
    int k = clampInt(opt<int>("k", 4), 1, 26);
    int forcedType = opt<int>("type", -1); 
    long long seedVal = 0;
    if (argc >= 2) {
        try {
            seedVal = stoll(string(argv[1]));
        } catch (...) {
            seedVal = 0;
        }
    }

    int n;
    string s;

    if (seedVal == 1) {
        n = 1;
        s = "a";
    } else if (seedVal == 2) {
        n = 100;
        s.assign(n, 'a');
        s[n / 2] = 'b';
        s[0] = 'a';
        s[n - 1] = 'a';
    } else if (seedVal == 3) {
        n = 100;
        s = makeAlternating(n, 2);
        s[49] = 'a';
        s[50] = 'b';
    } else {
        if (forcedN != -1) n = clampInt(forcedN, 1, 100);
        else {
            int roll = rnd.next(1, 100);
            if (roll <= 70) n = 100;
            else if (roll <= 90) n = rnd.next(85, 100);
            else n = rnd.next(60, 100);
        }

        int type = forcedType;
        if (type < 0 || type > 3) type = rnd.next(0, 3);

        if (type == 0) s = makeBlocks(n, k);
        else if (type == 1) s = makePalindromeWithNoise(n, k);
        else if (type == 2) s = makeAlternating(n, k);
        else s = makeAdversarialMix(n, k);

        if ((int)s.size() != n) s.resize(n, 'a');
        for (char& ch : s) {
            if (ch < 'a' || ch > 'z') ch = 'a';
        }
    }

    if (forcedN != -1) {
        n = clampInt(forcedN, 1, 100);
        if ((int)s.size() != n) {
            if ((int)s.size() < n) s += string(n - (int)s.size(), 'a');
            else s.resize(n);
        }
    }

    if (mode == "non") {
        cout << n << "\n" << s << "\n";
    } else if (mode == "adp") {
        cout << n << "\n";
    } else {
        quitf(_fail, "Unknown --mode (expected non/adp)");
    }
    return 0;
}