Submission #1097346

#TimeUsernameProblemLanguageResultExecution timeMemory
1097346anhthiThree Friends (BOI14_friends)C++14
100 / 100
111 ms30704 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
#define fi first
#define se second
#define ll long long
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define pb push_back
#define max_rng *max_element
#define min_rng *min_element
#define rep(i, n) for(int i = 1, _n = (n); i <= _n; ++i)
#define forn(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define ford(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
 
template <class X, class Y>
    inline bool maximize(X &x, Y y) {
        return (x < y ? x = y, true : false);
    }
 
template <class X, class Y>
    inline bool minimize(X &x, Y y) {
        return (x > y ? x = y, true : false);
    }
 
template <class X>
    inline void compress(vector<X> &a) {
        sort(all(a));
        a.resize(unique(all(a)) - a.begin());
    }
 
int ctz(ll x) { return __builtin_ctzll(x); }
int lg(ll x) { return 63 - __builtin_clzll(x); }
int popcount(ll x) { return __builtin_popcount(x); }
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) {
    return l + abs((ll) rng()) % (r - l + 1);
}
 
const ll oo = (ll) 1e17;
const int inf = (ll) 1e9 + 7, mod = (ll) 1e9 + 7;
 
const int N = 2e6 + 5, M = 2e5 + 5, BASE = 307, LOG = 23;
 
void add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
void sub(int &x, int y) { x -= y; if (x < 0) x += mod; }

int n;
string s;

int pw[N], pf[N];

int getRange(int i, int j) {
    return (pf[j] - 1LL * pf[i - 1] * pw[j - i + 1] + 1LL * mod * mod) % mod;
}

int main(void) {
 
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> n >> s;
    s = '!' + s;

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

    pw[0] = 1;
    rep(i, n) pw[i] = 1LL * pw[i - 1] * BASE % mod;

    pf[0] = 0;
    rep(i, n) pf[i] = (1LL * pf[i - 1] * BASE + s[i] - 'A' + 1) % mod;

    // cout << getRange(6, 9) << '\n';
    // cout << getRange(1, 4) << '\n';

    int pos = -1;
    int len = (n - 1) / 2;
    vector<int> check_unique;
    forn(i, 1, n) {
        bool ok = 0;
        if (i == 1) {
            int first = getRange(2, len + 1);
            if (getRange(2, len + 1) == getRange(len + 2, n)) {
                ok = 1;
                check_unique.pb(first);
            }
        }
        else if (i == n) {
            int first = getRange(1, len);
            if (getRange(1, len) == getRange(len + 1, n - 1)) {
                ok = 1;
                check_unique.pb(first);
            }
        }
        else {
            if (i <= len) {
                int first = getRange(1, i - 1);
                first = (1LL * pw[len - i + 1] * first + 1LL * getRange(i + 1, len + 1)) % mod;
                if (first == getRange(len + 2, n)) {
                    ok = 1;
                    check_unique.pb(first);
                } 
            }
            else {
                int second;
                if (i == len + 1) {
                    second = getRange(i + 1, n);
                }
                else {
                    second = (1LL * pw[n - i] * getRange(len + 1, i - 1) + 1LL * getRange(i + 1, n)) % mod;
                }
                if (getRange(1, len) == second) {
                    ok = 1;
                    check_unique.pb(second);
                }
            }
        }
        if (ok) {
            pos = i;
        }
    }
    if (pos == -1) {
        return cout << "NOT POSSIBLE", 0;
    }  

    compress(check_unique);
    if (sz(check_unique) > 1) return cout << "NOT UNIQUE", 0;

    forn(i, 1, n) {
        if (i == pos) continue;
        if (!len) continue;    
        cout << s[i];
        len--;
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...