Submission #648545

#TimeUsernameProblemLanguageResultExecution timeMemory
648545ghostwriterThree Friends (BOI14_friends)C++14
35 / 100
11 ms7140 KiB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Tran The Bao - ghostwriter
    Training for VOI23 gold medal
----------------------------------------------------------------
    DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
const pi M = {1e9 + 7, 1e9 + 9};
const pi base = {37, 127};
const int N = 2e5 + 5;
int n, pos = 0;
char s[N];
pi h[N], p[N];
pi rs;
pi geth(int l, int r) {
	if (l > r) return {0, 0};
	pi rs;
	rs.st = (h[r].st - 1LL * h[l - 1].st * p[r - l + 1].st % M.st + M.st) % M.st;
	rs.nd = (h[r].nd - 1LL * h[l - 1].nd * p[r - l + 1].nd % M.nd + M.nd) % M.nd;
	return rs;
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    cin >> n;
    p[0] = {1, 1};
    FOR(i, 1, n) {
    	cin >> s[i];
    	p[i].st = 1LL * p[i - 1].st * base.st % M.st;
    	p[i].nd = 1LL * p[i - 1].nd * base.nd % M.nd;
    	h[i].st = (1LL * h[i - 1].st * base.st % M.st + s[i] - 'a' + 1) % M.st;
    	h[i].nd = (1LL * h[i - 1].nd * base.nd % M.nd + s[i] - 'a' + 1) % M.nd;
    }
    if (n % 2 == 0) {
    	cout << "NOT POSSIBLE";
    	return 0;
    }
    FOR(i, 1, n) {
    	pi h, h1;
    	if (i <= n / 2 + 1) {
    		pi pre = geth(1, i - 1), suf = geth(i + 1, n / 2 + 1);
    		h.st = (1LL * pre.st * p[n / 2 + 1 - i].st % M.st + suf.st) % M.st;
    		h.nd = (1LL * pre.nd * p[n / 2 + 1 - i].nd % M.nd + suf.nd) % M.nd;
    		h1 = geth(n / 2 + 2, n);
    	}
    	else {
    		pi pre = geth(n / 2 + 1, i - 1), suf = geth(i + 1, n);
    		h1.st = (1LL * pre.st * p[n - i].st % M.st + suf.st) % M.st;
    		h1.nd = (1LL * pre.nd * p[n - i].nd % M.nd + suf.nd) % M.nd;
    		h = geth(1, n / 2);
    	}
    	if (h == h1) {
    		if (pos && rs != h) {
    			cout << "NOT UNIQUE";
    			return 0;
    		}
    		rs = h;
    		pos = i;
    	}
    }
    if (!pos) cout << "NOT POSSIBLE";
    else if (pos <= n / 2 + 1) FOR(i, n / 2 + 2, n) cout << s[i];
    else FOR(i, 1, n / 2) cout << s[i];
    return 0;
}
/*
----------------------------------------------------------------
From Benq:
    stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/

Compilation message (stderr)

friends.cpp: In function 'int main()':
friends.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
friends.cpp:71:5: note: in expansion of macro 'FOR'
   71 |     FOR(i, 1, n) {
      |     ^~~
friends.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
friends.cpp:82:5: note: in expansion of macro 'FOR'
   82 |     FOR(i, 1, n) {
      |     ^~~
friends.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
friends.cpp:106:32: note: in expansion of macro 'FOR'
  106 |     else if (pos <= n / 2 + 1) FOR(i, n / 2 + 2, n) cout << s[i];
      |                                ^~~
friends.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
friends.cpp:107:10: note: in expansion of macro 'FOR'
  107 |     else FOR(i, 1, n / 2) cout << s[i];
      |          ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...