Submission #1165778

#TimeUsernameProblemLanguageResultExecution timeMemory
1165778fryingducThree Friends (BOI14_friends)C++20
0 / 100
8 ms5296 KiB
#include "bits/stdc++.h"

using namespace std;

#ifdef duc_debug
#include "bits/debug.h"
#else
#define debug(...)
#endif

const int maxn = 2e5 + 5;
const int mod = 1e9 + 9;
const int base = 31;
int n;
string s;
int h[maxn], p[maxn];

void solve() {
  cin >> n >> s;
  if (n % 2 == 0) {
    cout << "NOT POSSIBLE\n";
    return;
  }
  s = ' ' + s;
  for (int i = 1; i <= n; ++i) {
    h[i] = ((1ll * h[i - 1] * base % mod) + (s[i] - 'A' + 1)) % mod;
  }
  auto get_hash = [&](int l, int r) -> int {
    if (l > r) return 0;
    return (h[r] - (1ll * h[l - 1] * p[r - l + 1] % mod) + mod) % mod;
  };
  vector<int> pos;
  for (int i = 1; i <= n; ++i) {
    int hh = 0;
    if (i <= n / 2) {
      hh = get_hash(n / 2 + 2, n);
    } else {
      hh = h[n / 2];
    }
    int rm = (1ll * get_hash(1, i - 1) * p[n - i] % mod + get_hash(i + 1, n)) % mod;
    hh = (1ll * hh * p[n / 2] % mod + hh) % mod;
    if (rm == hh) {
      pos.push_back(i);
    }
  }
  if (pos.empty()) {
    cout << "NOT POSSIBLE\n";
  } else if ((int)pos.size() > 1) {
    cout << "NOT UNIQUE\n";
  } else {
    s.erase(s.begin() + pos[0]);
    for (int i = 1; i <= n / 2; ++i) {
      cout << s[i];
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  
  p[0] = 1;
  for (int i = 1; i < maxn; ++i) {
    p[i] = 1ll * p[i - 1] * base % mod;
  }
  solve();

  return 0;
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...