Submission #938215

#TimeUsernameProblemLanguageResultExecution timeMemory
938215Faisal_SaqibThree Friends (BOI14_friends)C++17
100 / 100
170 ms34868 KiB
#include <iostream> #include <set> using namespace std; #define ll long long const ll N=2e6+2; ll pre[N],pw[N],base=379,mod=1e9+9; int get(int l,int r)// one base indexing 1<=l<=n { if(r<l) return 0; return ((pre[r]-(pre[l-1]*pw[r-l+1])%mod)%mod+mod)%mod; } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); int n; cin>>n; string s; cin>>s; pre[0]=0; pw[0]=1; for(int i=1;i<=n;i++) pw[i]=(pw[i-1]*base)%mod; for(int i=1;i<=n;i++) pre[i]=((pre[i-1]*base)%mod+(s[i-1]-'A'))%mod; int posa=-1; int pp; int len=(n-1)/2; for(int i=1;(n%2==1) and i<=n;i++) { ll fit=0; ll sec=0; int lf=(i-1); if(lf<=len) { fit=((get(1,i-1)*pw[len-lf])%mod+get(i+1,i+len-lf))%mod; sec=get(i+len-lf+1,n); } else{ fit=get(1,len); sec=((get(len+1,i-1)*pw[n-i])%mod + get(i+1,n))%mod; } if(fit==sec) { if(posa==-1) { pp=i; posa=fit; } else if(posa!=fit) { posa=-2; break; } } } if(posa==-1 or n%2!=1) { cout<<"NOT POSSIBLE\n"; } else if(posa!=-2) { int j=(pp); for(int i=1,p=0;p<len;i++) { if(i!=j) { cout<<s[i-1]; p++; } } cout<<'\n'; } else{ cout<<"NOT UNIQUE\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...