Submission #821829

#TimeUsernameProblemLanguageResultExecution timeMemory
821829serifefedartarThree Friends (BOI14_friends)C++17
0 / 100
79 ms34696 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;

    ll leftmostH = hash_ss(1, len);
    ll rightmostH = hash_ss(N-len+1, N);

    if (leftmostH == rightmostH) {
        for (int i = 1; i <= len; i++)
            cout << S[i];
        cout << "\n";
        return 0;
    }

    ll left_hash, right_hash; 
    for (int i = 1; i <= N/2; i++) {
        if (i == 1)
            left_hash = hash_ss(2, N/2+1);
        else
            left_hash = hash_concat(1, i - 1, i + 1, N/2+1);

        if (left_hash == rightmostH)
            right = true;
    }

    for (int i = N/2+1; i <= N; i++) {
        if (i == N)
            right_hash = hash_ss(N/2, N-1);
        else
            right_hash = hash_concat(N/2, i - 1, i + 1, N); 

        if (right_hash == leftmostH)
            left = 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)
        cout << "NOT UNIQUE\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...