# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
971667 | batsukh2006 | Three Friends (BOI14_friends) | C++17 | 44 ms | 6496 KiB |
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<iostream>
#include<stdio.h>
#include<math.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<string.h>
#include<utility>
#include<set>
#include<cmath>
#include<queue>
#include<deque>
#include<functional>
#include<stack>
#include<limits.h>
#include<iomanip>
#include<unordered_map>
#include<numeric>
#include<tuple>
#include<bitset>
using namespace std;
#define MOD 1000000007
#define int long long
#define endl '\n'
void solve(){
int n; cin>>n;
vector<char> u(n+1);
for(int i=1; i<=n; i++) cin>>u[i];
if(n%2==0){
cout<<"NOT POSSIBLE";
}else{
int h=n/2;
int cnt=0;
vector<int> mp(26);
for(int i=1; i<=h; i++){
if(u[i]==u[i+h]) cnt++;
else break;
}
for(int i=1; i<=n; i++) mp[u[i]-'A']++;
int zero=0;
for(int i=0; i<26; i++){
if(mp[i]>0) zero++;
}
if(zero==1){
for(int i=1; i<=n/2; i++) cout<<u[i];
}else if(cnt==h&&u[1]==u[n]){
cout<<"NOT UNIQUE";
}else{
int one=0,war=0,zero=0;
for(int i=0; i<26; i++){
if(mp[i]==1) one++;
if(mp[i]%2==1) war++;
}
if(war>1){
cout<<"NOT POSSIBLE";
}else{
if(one==1){
vector<int> v;
for(int i=1; i<=n; i++){
if(mp[u[i]-'A']!=1) v.push_back(u[i]-'A');
}
int need=0;
for(int i=0; i<n/2; i++){
if(v[i]==v[i+h]) need++;
}
if(need==n/2){
vector<char> s;
for(int i=0; i<n/2; i++){
char c=v[i]+'A';
s.push_back(c);
}
for(int i=0; i<s.size(); i++) cout<<s[i];
}else{
cout<<"NOT POSSIBLE";
}
}else{
vector<char> s;
int i=1,j=n/2+1;
while(i<=n/2+1&&j<=n){
if(u[i]==u[j]){
s.push_back(u[i]);
i++;
j++;
}else{
if(mp[u[i]-'A']%2==1){
i++;
}else if(mp[u[j]-'A']%2==1){
j++;
}else{
break;
}
}
}
if(s.size()==n/2){
for(int i=0; i<s.size(); i++) cout<<s[i];
}else{
s.clear();
int i=1,j=n/2-1;
while(i<=n/2&&j<=n){
if(u[i]==u[j]){
s.push_back(u[i]);
i++;
j++;
}else{
if(mp[u[i]-'A']%2==1){
i++;
}else if(mp[u[j]-'A']%2==1){
j++;
}else{
war=1;
break;
}
}
}
if(s.size()==n/2){
for(int i=0; i<s.size(); i++) cout<<s[i];
}else{
s.clear();
int i=1,j=n/2+2;
while(i<=n/2+1&&j<=n){
if(u[i]==u[j]){
s.push_back(u[i]);
i++;
j++;
}else{
if(mp[u[i]-'A']%2==1){
i++;
}else if(mp[u[j]-'A']%2==1){
j++;
}else{
war=1;
break;
}
}
}
if(s.size()==n/2){
for(int i=0; i<s.size(); i++) cout<<s[i];
}else{
cout<<"NOT POSSIBLE";
return;
}
}
}
}
}
}
}
}
signed main(){
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T=1;
// cin>>T;
while(T--){
solve();
cout<<endl;
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |