Submission #78671

#TimeUsernameProblemLanguageResultExecution timeMemory
78671thebesThree Friends (BOI14_friends)C++14
100 / 100
166 ms38732 KiB
#include <bits/stdc++.h> using namespace std; const int MN = 2e6+6; int h[MN], pw[MN]={1}, N, L, i, cnt; string s, ans; set<int> lol; int hsh(int l,int r){return h[r]-h[l-1]*pw[r-l+1];} int main(){ cin >> N >> s; for(i=1;i<=N;i++){ h[i]=h[i-1]*131+s[i-1]-'A'; pw[i]=pw[i-1]*131; } if(N%2){ L = N/2; for(i=1;i<=N;i++){ if(i<L+1){ int l1 = i-1; int h1 = hsh(1,l1)*pw[L-l1]+hsh(i+1,i+L-l1); int h2 = hsh(N-L+1,N); if(h1==h2){ cnt++; lol.insert(hsh(N-L+1,N)); if(cnt == 1){ for(int j=N-L+1;j<=N;j++) ans.push_back(s[j-1]); } } } else if(i>L+1){ int h1 = hsh(1,L); int h2 = hsh(L+1,i-1)*pw[N-i]+hsh(i+1,N); if(h1==h2){ cnt++; lol.insert(hsh(1,L)); if(cnt == 1){ for(int j=1;j<=L;j++) ans.push_back(s[j-1]); } } } else{ if(hsh(1,i-1)==hsh(i+1,N)){ cnt++; lol.insert(hsh(1,L)); if(cnt == 1){ for(int j=1;j<=L;j++) ans.push_back(s[j-1]); } } } } if(cnt==0) printf("NOT POSSIBLE\n"); else if(lol.size()>1) printf("NOT UNIQUE\n"); else printf("%s\n",ans.c_str()); } else printf("NOT POSSIBLE\n"); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...