제출 #135705

#제출 시각아이디문제언어결과실행 시간메모리
135705VlatkoThree Friends (BOI14_friends)C++14
0 / 100
174 ms56620 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll mod = 1000000007;
const ll p = 31;
const ll pinv = 129032259;

const int MAXN = 2000010;

int N, ans1, ans2;
int S[MAXN];
ll h[MAXN];
ll ppow[MAXN];
ll ppowinv[MAXN];

ll subhash(int a, int b) {
	if (a > b) return 0;
	ll res = h[b] - h[a-1];
	if (res < 0) res += mod;
	res = (res * ppowinv[a]) % mod; 
	return res;
}

ll combine(int a, int b, int c, int d) {
	ll x = subhash(a, b);
	ll y = subhash(c, d);
	y = (y * ppow[b-a+1]) % mod;
	return (x + y) % mod;
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> N;
	if (N % 2 == 0) {
		cout << "NOT POSSIBLE\n";
		return 0;
	}
	for (int i = 1; i <= N; ++i) {
		char ch;
		cin >> ch;
		S[i] = ch - 'A' + 1;
	}
	ppow[0] = ppowinv[0] = 1;
	for (int i = 1; i <= N; ++i) {
		ppow[i] = (ppow[i-1] * p) % mod;
		ppowinv[i] = (ppowinv[i-1] * pinv) % mod;
	}
	h[0] = 0;
	for (int i = 1; i <= N; ++i) {
		h[i] = (h[i-1] + ppow[i]*S[i]) % mod;
	}
	ans1 = -1;
	for (int i = 1; i <= N; ++i) {
		if (i <= N/2) {
			if (combine(1, i-1, i+1, (N/2)+1) == subhash((N/2)+2, N)) {
				if (ans1 != -1) {
					cout << "NOT UNIQUE\n";
					return 0;
				}
				ans1 = (N/2)+2;
				ans2 = N;
			}
		} else if (i == (N/2)+1) {
			if (subhash(1, i-1) == subhash(i+1, N)) {
				if (ans1 != -1) {
					cout << "NOT UNIQUE\n";
					return 0;
				}
				ans1 = 1;
				ans2 = N/2;
			}
		} else {
			if (subhash(1, N/2) == combine((N/2)+1, i-1, i+1, N)) {
				if (ans1 != -1) {
					cout << "NOT UNIQUE\n";
					return 0;
				}
				ans1 = 1;
				ans2 = N/2;
			}
		}
	}
	if (ans1 == -1) {
		cout << "NOT POSSIBLE\n";
	} else {
		for (int i = ans1; i <= ans2; ++i) {
			cout << char(S[i] + 'A' - 1);
		}
		cout << "\n";
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...