Submission #380419

#TimeUsernameProblemLanguageResultExecution timeMemory
380419SavicSThree Friends (BOI14_friends)C++14
0 / 100
123 ms50468 KiB
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
 
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)
 
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 2000005;
const int inf = 1e9 + 5;
 
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k)  print the k-th smallest number in os(0-based)
 
int n;
string s;
 
// Hashing - O(n)
const int mod = 1e9 + 7;
ll add(ll a, ll b){
    return (a + b) % mod;
}
ll sub(ll a, ll b){
	return (a - b + mod) % mod;
}
ll mul(ll a, ll b){
    return (a * b) % mod;
}
ll power(ll a, ll b){
    if(!b)return 1;
    ll pola = power(a, b / 2);
    pola = mul(pola, pola);
    if(b % 2)pola = mul(pola, a);
    return pola;
}
ll inv(ll a){
    return power(a, mod - 2);
}
 
const int p = 31;
ll pw[maxn];
ll invz[maxn];
void init_hsh(){
	pw[0] = 1;
	ff(i,1,maxn - 1)pw[i] = mul(pw[i - 1], p);
	invz[maxn - 1] = inv(pw[maxn - 1]);
	fb(i,maxn - 2, 0)invz[i] = mul(invz[i + 1], p);
}
 
ll hsh[maxn];
ll get(int l, int r){
	if(l > r)return 0;
	return mul(sub(hsh[r], hsh[l - 1]), invz[l - 1]);
}
 
int main()
{
    ios::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
	cin >> n >> s;
	if(n % 2 == 0)return cout << "NOT POSSIBLE", 0;
	init_hsh();
	ff(i,0,n - 1)hsh[i + 1] = add(hsh[i], mul(pw[i], s[i] - 'A' + 1));
	int len = (n - 1) / 2;
	
	int rez = 0;
	int L = 0, R = -1;
	ff(i,1,len){
		// brisem i-ti
		ll a1 = hsh[i - 1];
		ll a2 = mul(pw[i - 1], get(i + 1, len + 1));
		ll a = a1 + a2;
		ll b = get(len + 2, n);
		if(a == b){
			rez += 1;
			L = len + 2;
			R = n;
		}
	}
	
	ff(i,len + 1,n){
		// brisem i-ti
		ll a1 = get(len + 1, i - 1);
		ll a2 = mul(pw[i - len - 1], get(i + 1, n));
		ll a = a1 + a2;
		ll b = hsh[len];
		if(a == b){
			rez += 1;
			L = 1;
			R = len;
		}
	}
	
	if(rez == 0){
		cout << "NOT POSSIBLE" << endl;
		return 0;
	}
	
	if(rez > 1){
		cout << "NOT UNIQUE" << endl;
		return 0;
	}
	ff(i,L - 1,R - 1)cout << s[i];
	cout << endl;
	
    return 0;
}
/**
 
 
 
// probati bojenje sahovski ili slicno
 
**/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...