Submission #135574

#TimeUsernameProblemLanguageResultExecution timeMemory
135574tdwnThree Friends (BOI14_friends)C++17
0 / 100
199 ms10992 KiB
#include <bits/stdc++.h> #define ll long long #define pb push_back #define mp make_pair using namespace std; const int maxn = 200100; const ll hashmod = 1e9 + 9; const ll k = 37LL; ll pref_hash[maxn], n; ll inv[maxn]; string str; ll power(ll a, ll b) { if(b == 0) return 1LL; else if(b == 1) return 1LL * a; else { if(b % 2 == 0) { ll temp = power(a, b/2); return (temp*temp)%hashmod; } else return (a*power(a, b-1))%hashmod; } } ll get_hash(int l, int r) { ll diff = pref_hash[r] + hashmod - pref_hash[l-1]; diff %= hashmod; diff *= inv[l-1]; diff %= hashmod; return diff; } int main() { cin>>n; cin>>str; //ofstream fout("output.out"); if(n % 2 == 0) { cout<<"NOT POSSIBLE\n"; return 0; } ll kk = k; inv[0] = 1LL; for(int i=1;i<=n;i++) { pref_hash[i] = pref_hash[i-1] + ((str[i-1]-'A'+1)*kk)%hashmod; pref_hash[i] %= hashmod; inv[i] = power(kk, hashmod-2); kk *= k; kk %= hashmod; } //cout<<get_hash(1, 2)<<" and "<<get_hash(5, 6)<<"\n"; ll half_sz = n / 2; ll mid = (n+1)/2; ll sz = n / 2; vector<int>v; for(int i=1;i<=n;i++) { if(i == 1 && get_hash(2, 1+sz) == get_hash(2+sz, n)) v.pb(i); else if(i == n && get_hash(1, sz) == get_hash(sz+1, n-1)) v.pb(i); else if(i == mid && get_hash(1, sz) == get_hash(mid, n)) v.pb(i); else if(i < mid) { ll lft_sz = i-1; ll rgh_sz = mid - i; if(get_hash(1, lft_sz) == get_hash(mid+1, mid+lft_sz) && get_hash(i+1, i+rgh_sz) == get_hash(n-rgh_sz+1, n)) v.pb(i); } else if(i > mid){ ll lft_sz = i - mid; ll rgh_sz = n - i; if(get_hash(1, lft_sz) == get_hash(mid, mid+lft_sz-1) && get_hash(lft_sz+1, mid-1) == get_hash(n - rgh_sz + 1, n)) v.pb(i); } } if(v.size() == 0) { cout<<"NOT POSSIBLE\n"; return 0; } else if(v.size() > 1) { cout<<"NOT UNIQUE\n"; return 0; } for(int i:v) { //cout<<i<<"\n"; if(i == 1) for(int j=mid+1;j<=n;j++) cout<<str[j-1]; else if(i == n) for(int j=1;j<mid;j++) cout<<str[j-1]; else if(i == mid) for(int j=n-half_sz+1;j<=n;j++) cout<<str[j-1]; else if(i < mid) { //cout<<n-half_sz+1<<"\n"; for(int j=n-half_sz+1;j<=n;j++) cout<<str[j-1]; } else if(i > mid) for(int j=1;j<mid;j++) cout<<str[j-1]; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...