Submission #820115

#TimeUsernameProblemLanguageResultExecution timeMemory
820115serifefedartarThree Friends (BOI14_friends)C++17
35 / 100
1079 ms35060 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MOD 1000000009
#define LOGN 20
#define MAXN 2000100

const ll A = 9973;
ll powA[MAXN];
ll hashes[MAXN];
ll hash_ss(int l, int r) {
    ll ans = (hashes[r] - (hashes[l-1] * powA[r-l + 1] % MOD)) % MOD;
    while (ans < 0)
        ans += MOD;
    return ans;
}

ll hash_concat(int l1, int r1, int l2, int r2) {
    ll ans = (hash_ss(l2, r2) + (hash_ss(l1, r1) * powA[r2 - l2 + 1] % MOD)) % MOD;
    return ans;
}

int cnt[26];
int main() {
    fast
    powA[0] = 1;
    for (int i = 1; i < MAXN; i++)
        powA[i] = (powA[i-1] * A) % MOD;

    int N;
    string S;
    cin >> N >> S;
    S = "#" + S;

    if (N % 2 == 0) {
        cout << "NOT POSSIBLE\n";
        return 0;
    }

    hashes[0] = 0;
    for (int i = 1; i <= N; i++)
        hashes[i] = (hashes[i-1] * A + (S[i] - 'A' + 1)) % MOD;

    for (int i = 1; i <= N; i++)
        cnt[S[i] - 'A']++;

    int selected_char = -1;
    for (int i = 0; i < 26; i++) {
        if (cnt[i] % 2 && selected_char != -1) {
            cout << "NOT POSSIBLE\n";
            return 0;
        }
        if (cnt[i] % 2)
            selected_char = i;
    }

    int len = N / 2;
    bool left = 0, right = 0;
    for (int i = 1; i <= N; i++) {
        if (S[i] - 'A' == selected_char) {
            ll left_hash = -1, right_hash = -1;

            if (i-1 >= len)
                left_hash = hash_ss(1, len);
            if (N-i >= len)
                right_hash = hash_ss(N-len+1, N);

            if (left_hash == -1) {
                if (i == 1)
                    left_hash = hash_ss(2, 1 + len);
                else
                    left_hash = hash_concat(1, i - 1, i + 1, N-len);
            }

            if (right_hash == -1) {
                if (i == N)
                    right_hash = hash_ss(N-len, N-1);
                else
                    right_hash = hash_concat(len + 1, i - 1, i + 1, N);
            }

            if (left_hash == right_hash) {
                if (i-1 >= len)
                    left = true;
                else
                    right = true;
            }
        }
    }

    if (!left && !right)
        cout << "NOT POSSIBLE\n";
    else if (left && !right) {
        for (int i = 1; i <= len; i++)
            cout << S[i];
        cout << "\n";
    } else if (right && !left) {
        for (int i = N-len+1; i <= N; i++)
            cout << S[i];
        cout << "\n";
    } else if (left && right) {
        string P = "", Q = "";
        for (int i = 1; i <= len; i++)
            P = P + S[i];
        for (int i = N-len+1; i <= N; i++)
            Q = Q + S[i];

        if (P == Q)
            cout << P << "\n";
        else
            cout << "NOT UNIQUE\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...