Submission #1332117

#TimeUsernameProblemLanguageResultExecution timeMemory
1332117kawhietThree Friends (BOI14_friends)C++20
100 / 100
50 ms35696 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define dbg(...) 47
#endif

#define int long long

constexpr int h = 41;
constexpr int mod = 1e9 + 7;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    string u;
    cin >> n >> u;
    if (n % 2 == 0) {
        cout << "NOT POSSIBLE\n";
        return 0;
    }
    vector<int> p(n + 1), pw(n + 1);
    pw[0] = 1;
    for (int i = 0; i < n; i++) {
        pw[i + 1] = (pw[i] * h) % mod;
        p[i + 1] = (p[i] * h + (u[i] - 'a' + 1)) % mod;
    }
    auto get = [&](int l, int r) {
        if (r < l) return 0LL;
        l++; r++;
        return (p[r] - p[l - 1] * pw[r - l + 1] % mod + mod) % mod;
    };
    int len = n / 2;
    int best_hash = -1, pos = -1;
    for (int i = 0; i < n; i++) {
        if (i <= len) {
            int x = (get(0, i - 1) * pw[len - i] + get(i + 1, len)) % mod;
            int y = get(n - len, n - 1);
            if (x == y) {
                if (best_hash != -1 && best_hash != y) {
                    cout << "NOT UNIQUE\n";
                    return 0;
                }
                pos = i;
                best_hash = y;
            }

        } else {
            int x = get(0, len - 1);
            int y = (get(len, i - 1) * pw[n - i - 1] + get(i + 1, n - 1)) % mod;
            if (x == y) {
                if (best_hash != -1 && best_hash != x) {
                    cout << "NOT UNIQUE\n";
                    return 0;
                }
                pos = i;
                best_hash = x;
            }
        }
    }
    if (best_hash == -1) {
        cout << "NOT POSSIBLE\n";
    } else {
        if (pos <= len) {
            cout << u.substr(n - len, len) << '\n';
        } else {
            cout << u.substr(0, len) << '\n'; 
        }
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...