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>
#define int long long
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int N=2e6+5,t=33,mod=1e9+7;
int pw[N];
int cnt=0;
vector <int> p1,p2;
bool ok(int l1,int r1,int l2,int r2){
if(l1>r1)return 1;
int h1=(p1[r1]-(p1[l1-1]*pw[r1-l1+1]));
if(h1<0)h1+=(abs(h1)/mod+1)*mod;
h1%=mod;
int h2=(p2[r2]-(p2[l2-1]*pw[r2-l2+1]));
if(h2<0)h2+=(abs(h2)/mod+1)*mod;
h2%=mod;
if(h1==h2)return 1;
return 0;
}
void calc(string x,string y){
p1.clear();
p2.clear();
p1.pb(0);p2.pb(0);
for(int i=1;i<x.size();i++){
p1.pb((p1[i-1]*t+x[i])%mod);
}
for(int i=1;i<y.size();i++){
p2.pb((p2[i-1]*t+y[i])%mod);
}
bool tt=0;
for(int i=1;i<x.size();i++){
if(ok(1,i-1,1,i-1) && ok(i+1,x.size()-1,i,x.size()-2)){
tt=1;
}
}
if(tt)cnt++;
}
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
int n;
string s;
cin>>n>>s;
if(n%2==0){
cout<<"NOT POSSIBLE\n";
return 0;
}
string x=" ",y=" ",a=" ",b=" ";
for(int i=0;i<n;i++){
if(i<=n/2)x+=s[i];
else y+=s[i];
if(i<n/2)a+=s[i];
else b+=s[i];
}
pw[0]=1;
for(int i=1;i<x.size();i++){
pw[i]=pw[i-1]*t%mod;
}
calc(x,y);
calc(b,a);
if(!cnt)cout<<"NOT POSSIBLE\n";
else if(cnt==1 or y==a){
y.erase(0,1);
cout<<y<<"\n";
}
else cout<<"NOT UNIQUE\n";
}
/*
*/
Compilation message (stderr)
friends.cpp: In function 'void calc(std::string, std::string)':
friends.cpp:27:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i=1;i<x.size();i++){
| ~^~~~~~~~~
friends.cpp:30:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i=1;i<y.size();i++){
| ~^~~~~~~~~
friends.cpp:34:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int i=1;i<x.size();i++){
| ~^~~~~~~~~
friends.cpp: In function 'int main()':
friends.cpp:59:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i=1;i<x.size();i++){
| ~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |