#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 2001000;
const ll hashmod = 1e9 + 9;
const ll k = 37LL;
ll pref_hash[maxn], n;
ll inv[maxn];
string str;
inline ll power(ll a, ll b) {
if(b == 0) return 1LL;
else if(b == 1) return 1LL * a;
else {
if(b % 2 == 0) {
ll temp = power(a, b/2);
return (temp*temp)%hashmod;
}
else return (a*power(a, b-1))%hashmod;
}
}
inline ll get_hash(int l, int r) {
ll diff = pref_hash[r] + hashmod - pref_hash[l-1];
if(diff > hashmod) diff -= hashmod;
diff *= inv[l-1];
//while(diff > hashmod) diff -= hashmod;
diff %= hashmod;
return diff;
}
int main() {
ios_base::sync_with_stdio(false);
cin>>n;
cin>>str;
if(n % 2 == 0) {
cout<<"NOT POSSIBLE\n";
return 0;
}
ll temp_k = power(k, hashmod-2);
ll kk = k;
inv[0] = 1LL;
for(int i=1;i<=n;i++) {
pref_hash[i] = pref_hash[i-1] + ((str[i-1]-'A'+1)*kk)%hashmod;
//if(pref_hash[i] > hashmod) pref_hash[i] -= hashmod;
pref_hash[i] %= hashmod;
inv[i] = inv[i-1] * temp_k;
inv[i] %= hashmod;
kk *= k; kk %= hashmod;
}
//cout<<get_hash(1, 2)<<" and "<<get_hash(5, 6)<<"\n";
ll half_sz = n / 2;
ll mid = (n+1)/2;
ll sz = n / 2;
bool dwn = false, upp = false;
vector<int>v;
for(int i=1;i<=n;i++) {
if(i == 1 && get_hash(2, 1+sz) == get_hash(2+sz, n)) {
if(i <= mid) dwn = true;
else upp = true;
v.pb(i);
}
else if(i == n && get_hash(1, sz) == get_hash(sz+1, n-1)) {v.pb(i); if(i<=mid) dwn = true; else upp = true;}
else if(i == mid && get_hash(1, sz) == get_hash(mid+1, n)){v.pb(i); if(i<=mid) dwn = true; else upp = true;}
else if(i < mid) {
ll lft_sz = i-1;
ll rgh_sz = mid - i;
if(pref_hash[lft_sz] == get_hash(mid+1, mid+lft_sz) && get_hash(i+1, i+rgh_sz) == get_hash(n-rgh_sz+1, n)){v.pb(i); if(i<=mid) dwn = true; else upp = true;}
}
else if(i > mid){
ll lft_sz = i - mid;
ll rgh_sz = n - i;
if(pref_hash[lft_sz] == get_hash(mid, mid+lft_sz-1) && get_hash(lft_sz+1, mid-1) == get_hash(n - rgh_sz + 1, n)) {v.pb(i); if(i<=mid) dwn = true; else upp = true;}
}
}
if(v.size() == 0) {
cout<<"NOT POSSIBLE\n";
return 0;
}
if(v.size() == 1) {
for(int i:v) {
if(i == 1) cout<<str.substr(mid, half_sz); //hashes.insert(get_hash(mid+1, n));
else if(i == n) cout<<str.substr(0, half_sz); //hashes.insert(get_hash(1, mid));
else if(i == mid) cout<<str.substr(mid, half_sz); //hashes.insert(get_hash(n-half_sz+1, n));
else if(i < mid) cout<<str.substr(mid, half_sz); //hashes.insert(get_hash(n-half_sz+1, n));
else if(i > mid) cout<<str.substr(0, half_sz); //hashes.insert(get_hash(1, mid));
return 0;
}
}
if(upp && dwn && get_hash(1, mid-1) != get_hash(mid+1, n)) {
cout<<"NOT UNIQUE\n";
}
else {
for(int i:v) {
if(i == 1) cout<<str.substr(mid, half_sz); //hashes.insert(get_hash(mid+1, n));
else if(i == n) cout<<str.substr(0, half_sz); //hashes.insert(get_hash(1, mid));
else if(i == mid) cout<<str.substr(mid, half_sz); //hashes.insert(get_hash(n-half_sz+1, n));
else if(i < mid) cout<<str.substr(mid, half_sz); //hashes.insert(get_hash(n-half_sz+1, n));
else if(i > mid) cout<<str.substr(0, half_sz); //hashes.insert(get_hash(1, mid));
return 0;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
380 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
380 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
380 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
2 ms |
376 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
2 ms |
376 KB |
Output is correct |
35 |
Correct |
2 ms |
376 KB |
Output is correct |
36 |
Correct |
2 ms |
376 KB |
Output is correct |
37 |
Correct |
2 ms |
376 KB |
Output is correct |
38 |
Correct |
2 ms |
376 KB |
Output is correct |
39 |
Correct |
2 ms |
376 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Correct |
2 ms |
376 KB |
Output is correct |
44 |
Correct |
2 ms |
376 KB |
Output is correct |
45 |
Correct |
2 ms |
376 KB |
Output is correct |
46 |
Correct |
2 ms |
376 KB |
Output is correct |
47 |
Correct |
2 ms |
376 KB |
Output is correct |
48 |
Correct |
2 ms |
376 KB |
Output is correct |
49 |
Correct |
2 ms |
376 KB |
Output is correct |
50 |
Correct |
2 ms |
376 KB |
Output is correct |
51 |
Correct |
2 ms |
376 KB |
Output is correct |
52 |
Correct |
2 ms |
376 KB |
Output is correct |
53 |
Correct |
2 ms |
376 KB |
Output is correct |
54 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
35680 KB |
Output is correct |
2 |
Correct |
78 ms |
35608 KB |
Output is correct |
3 |
Correct |
76 ms |
35736 KB |
Output is correct |
4 |
Correct |
87 ms |
35736 KB |
Output is correct |
5 |
Correct |
84 ms |
35608 KB |
Output is correct |
6 |
Correct |
8 ms |
4380 KB |
Output is correct |
7 |
Correct |
163 ms |
45428 KB |
Output is correct |
8 |
Correct |
90 ms |
32152 KB |
Output is correct |
9 |
Correct |
89 ms |
33764 KB |
Output is correct |
10 |
Correct |
65 ms |
33816 KB |
Output is correct |
11 |
Correct |
58 ms |
29852 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
380 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
380 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
2 ms |
504 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
2 ms |
376 KB |
Output is correct |
35 |
Correct |
2 ms |
376 KB |
Output is correct |
36 |
Correct |
2 ms |
376 KB |
Output is correct |
37 |
Correct |
2 ms |
376 KB |
Output is correct |
38 |
Correct |
2 ms |
376 KB |
Output is correct |
39 |
Correct |
2 ms |
376 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Correct |
2 ms |
376 KB |
Output is correct |
44 |
Correct |
2 ms |
376 KB |
Output is correct |
45 |
Correct |
2 ms |
376 KB |
Output is correct |
46 |
Correct |
2 ms |
376 KB |
Output is correct |
47 |
Correct |
2 ms |
376 KB |
Output is correct |
48 |
Correct |
2 ms |
376 KB |
Output is correct |
49 |
Correct |
2 ms |
376 KB |
Output is correct |
50 |
Correct |
2 ms |
376 KB |
Output is correct |
51 |
Correct |
2 ms |
376 KB |
Output is correct |
52 |
Correct |
2 ms |
376 KB |
Output is correct |
53 |
Correct |
2 ms |
380 KB |
Output is correct |
54 |
Correct |
2 ms |
380 KB |
Output is correct |