This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// __builtin_popcount(x) broj bitova
// __builtin_popcountll(x)
// __builtin_clz(x) vodece nule
// __builtin_clzll(x)
// __builtin_ctz(x) nule na pocetku
// __builtin_ctzll(x)
// 2000000011
// 2000000033
#include "bits/stdc++.h"
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define rall(a) a.begin(),a.end(),greater<int>()
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define pi 3.14159265358979323846
#define here cerr<<"---------------------------\n"
#define ceri(a,l,r) {for(ll i = l;i<=r;i++) cerr<<a[i]<< " ";cerr<<endl;}
#define ceri2(a,l,r,n,m) {for(ll i = l;i<=r;i++){for(ll j = n;j<=m;j++) cerr<<a[i][j]<< " ";cerr<<endl;}}
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
return uniform_int_distribution<ll>(l,r)(rng);
}
void setIO(string inoutname)
{
freopen((inoutname+".in").c_str(),"r",stdin);
freopen((inoutname+".out").c_str(),"w",stdout);
}
#define mod1 1000000007
#define mod2 2000000033
ll gcd(ll a, ll b)
{
if(b==0) return a;
if(a==0) return b;
if(a>=b) return gcd(a%b,b);
return gcd(a,b%a);
}
ll lcm(ll a,ll b){
return (a/gcd(a,b))*b;
}
ll add1(ll a,ll b){
a+=b;
if(a<0) a+=mod1;
if(a>=mod1) a%=mod1;
return a;
}
ll mul1(ll a,ll b){return(a*b)%mod1;}
ll add2(ll a,ll b){
a+=b;
if(a<0) a+=mod2;
if(a>=mod2) a%=mod2;
return a;
}
ll mul2(ll a,ll b){return(a*b)%mod2;}
#define p1 1009
#define p2 9973
#define maxn 2000005
ll n;
string s;
ll hsh1[maxn];
ll hsh2[maxn];
ll po1[maxn];
ll po2[maxn];
void hashuj1(){
hsh1[0] = s[0]-'A'+1;
for(ll i = 1;i<n;i++){
hsh1[i] = add1(mul1(hsh1[i-1],p1),s[i]-'A'+1);
}
}
void hashuj2(){
hsh2[0] = s[0]-'A'+1;
for(ll i = 1;i<n;i++){
hsh2[i] = add2(mul2(hsh2[i-1],p2),s[i]-'A'+1);
}
}
ll get1(ll l,ll r){
if(l>r) return 0;
if(l==0) return hsh1[r];
return add1(hsh1[r],-mul1(hsh1[l-1],po1[r-l+1]));
}
ll get2(ll l,ll r){
if(l>r) return 0;
if(l==0) return hsh2[r];
return add2(hsh2[r],-mul2(hsh2[l-1],po2[r-l+1]));
}
void tc(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
po1[0] = 1;
po2[0] = 1;
for(ll i = 1;i<=maxn-1;i++) po1[i] = mul1(po1[i-1],p1);
for(ll i = 1;i<=maxn-1;i++) po2[i] = mul2(po2[i-1],p2);
cin >> n;
cin >> s;
hashuj1();
hashuj2();
ll m = (n-1)/2;
if(n%2==0){
cout<<"NOT POSSIBLE"<<endl;
return;
}
ll cnt = 0;
ll poc = 0;
ll u1 = -1;
ll u2 = -1;
for(ll i = 0;i<n;i++){
if(i<m){
ll l1 = get1(m+1,n-1);
ll l2 = get2(m+1,n-1);
ll r1 = add1(mul1(po1[m-i],get1(0,i-1)),get1(i+1,m));
ll r2 = add2(mul2(po2[m-i],get2(0,i-1)),get2(i+1,m));
if(l1==r1&&l2==r2){
if(cnt>0&&(u1!=r1||u2!=r2)&&u1!=-1){
cout<<"NOT UNIQUE"<<endl;
return;
}
u1 = l1;
u2 = l2;
poc = m+1;
cnt = 1;
}
}else{
ll l1 = get1(0,m-1);
ll l2 = get2(0,m-1);
ll r1 = add1(mul1(po1[n-i-1],get1(m,i-1)),get1(i+1,n-1));
ll r2 = add2(mul2(po2[n-i-1],get2(m,i-1)),get2(i+1,n-1));
if(l1==r1&&l2==r2){
if(cnt>0&&(u1!=l1||u2!=l2)&&u1!=-1){
cout<<"NOT UNIQUE"<<endl;
return;
}
u1 = l1;
u2 = l2;
poc = 0;
cnt = 1;
}
}
}
if(cnt>1) cout<<"NOT UNIQUE"<<endl;
if(cnt==0) cout<<"NOT POSSIBLE"<<endl;
if(cnt==1) cout<<s.substr(poc,m)<<endl;
}
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
//setIO("lol");
int t; t = 1;
while(t--){
tc();
}
return 0;
}
Compilation message (stderr)
friends.cpp: In function 'void setIO(std::string)':
friends.cpp:47:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | freopen((inoutname+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
friends.cpp:48:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | freopen((inoutname+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |