This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 9;
const int C = 2017;
ll modpow(ll a, ll b)
{
if(b<0) b+=(MOD-1);
ll r= 1;
while(b)
{
if(b&1) r = (r*a)%MOD;
a=(a*a)%MOD;
b>>=1;
}
return r;
}
ll inv(ll x)
{
return modpow(x, MOD - 2);
}
ll invC;
ll dp[2000001];
ll hsh(int l, int r)
{
if(r<l) return 0;
r++; l++;
return ((dp[r] - dp[l-1])%MOD+MOD)%MOD;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
//freopen("friend.in", "r", stdin);
invC = inv(C);
int n; string s;
cin >> n >> s;
if(n%2==0)
{
cout << "NOT POSSIBLE";
return 0;
}
int l = n/2;
vi pos;
//string a = s.substr(0, l);
//string b = s.substr(l, l);
//if(a == b) pos = n - 1;
bool started = false;
bool flag = true; char theletter = '$';
ll CMOD = modpow(C,l+1);
for(int i = 1; i <= n; i++)
{
dp[i] = (dp[i-1] + ll(s[i-1]-'A'+1)*modpow(C, n - i))%MOD;
}
for(int i = 0; i < n; i++) //it's placed at pos i
{
if(started)
{
if(s[i] != theletter) flag = false;
}
if(i < l)
{
ll h1 = hsh(0,i-1);
ll h2 = hsh(i+1,l);
h2 = (h2*C)%MOD;
h1 += h2;
h1%=MOD;
if(h1 == (hsh(l+1,n-1)*CMOD)%MOD)
{
pos.pb(i);
if(!flag)
{
cout << "NOT UNIQUE";
return 0;
}
if(!started)
{
started = true;
theletter = s[i];
}
}
}
if(i == l)
{
if(hsh(0,l-1)==(hsh(l+1,n-1)*CMOD)%MOD)
{
pos.pb(i);
if(!flag)
{
cout << "NOT UNIQUE";
return 0;
}
if(!started)
{
started = true;
theletter = s[i];
}
}
}
if(i > l)
{
ll h1 = hsh(l,i-1);
ll h2 = hsh(i+1,n-1);
h1=(h1*invC)%MOD;
h1=(h1+h2)%MOD;
if(hsh(0,l-1)==(h1*CMOD)%MOD)
{
pos.pb(i);
if(!flag)
{
cout << "NOT UNIQUE";
return 0;
}
if(!started)
{
started = true;
theletter = s[i];
}
}
}
}
if(pos.empty())
{
cout << "NOT POSSIBLE"; return 0;
}
/*
char commonletter = s[pos[0]];
for(int i = 0; i < pos.size(); i++)
{
if(s[pos[i]] != commonletter)
{
cout << "NOT POSSIBLE"; return 0;
}
}
*/
int cnt = 0;
for(int i = 0; i < pos[0]; i++)
{
if(cnt >= l) break;
cout << s[i];
cnt++;
}
for(int i = pos[0] + 1; i < n; i++)
{
if(cnt >= l) break;
cout << s[i];
cnt++;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |