제출 #1332107

#제출 시각아이디문제언어결과실행 시간메모리
1332107kawhietThree Friends (BOI14_friends)C++20
35 / 100
1097 ms36656 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;
    string s;
    int 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) {
                string cur = u.substr(n - len, len);
                if (!s.empty() && cur != s) {
                    cout << "NOT UNIQUE\n";
                    return 0;
                }
                s = cur;
            }
        } 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) {
                string cur = u.substr(0, len);
                if (!s.empty() && cur != s) {
                    cout << "NOT UNIQUE\n";
                    return 0;
                }
                s = cur;
            }
        }
    }
    if (s.empty()) {
        cout << "NOT POSSIBLE\n";
    } else {
        cout << s << '\n';
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...