Submission #852937

#TimeUsernameProblemLanguageResultExecution timeMemory
852937lovrotThree Friends (BOI14_friends)C++17
100 / 100
113 ms36556 KiB
#include <cstdio>

using namespace std;

typedef long long ll; 

const int N = 2e6 + 10;
const ll B = 101;

int n;
char STR[N];

ll H[N], P[N];

int main() {
	scanf("%d", &n);
	P[0] = 1;
	for(int i = 1; i <= n; ++i) {	
		P[i] = P[i - 1] * B;
		scanf(" %c", STR + i);
		H[i] = H[i - 1] + P[i - 1] * (ll) (STR[i] - 'A' + 1);
	}
	if(n == 1 || (n & 1) == 0) { printf("NOT POSSIBLE\n"); return 0; }
	int m = n >> 1;
	int l = -1, r = -1;
	ll h = -1;
	for(int i = 1; i <= n; ++i) {
		if(i > m) {
			ll a = H[m] * P[n - m];
			ll b = H[n] - H[i] + (H[i - 1] - H[m]) * B;
			if(a == b) {
				if(h != -1 && b != h) { printf("NOT UNIQUE\n"); return 0; }
				l = 1;
				r = m;
				h = b;
			}
		} else {
			ll a = H[n] - H[m + 1];
			ll b = (H[m + 1] - H[i] + H[i - 1] * B) * P[n - m - 1];
//			if(i == 3) printf("%d %d\n", a, b);
			if(a == b) {
				if(h != -1 && b != h) { printf("NOT UNIQUE\n"); return 0; }
				l = m + 2;
				r = n;
				h = b;
			}
		}
	}
	if(l == -1) printf("NOT POSSIBLE\n");
	else {
		for(int i = l; i <= r; ++i) printf("%c", STR[i]);
		printf("\n");
	}
	return 0;
}

Compilation message (stderr)

friends.cpp: In function 'int main()':
friends.cpp:16:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
friends.cpp:20:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |   scanf(" %c", STR + i);
      |   ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...