Submission #135651

#TimeUsernameProblemLanguageResultExecution timeMemory
135651tdwnThree Friends (BOI14_friends)C++17
35 / 100
681 ms35672 KiB
#include <bits/stdc++.h> #define ll long long #define pb push_back #define mp make_pair using namespace std; const int maxn = 2001000; const ll hashmod = 1e9 + 9; const ll k = 37LL; ll pref_hash[maxn], n; ll inv[maxn]; string str; inline 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; } } inline ll get_hash(int l, int r) { ll diff = pref_hash[r] + hashmod - pref_hash[l-1]; if(diff > hashmod) diff -= hashmod; diff *= inv[l-1]; //while(diff > hashmod) diff -= hashmod; diff %= hashmod; return diff; } int main() { ios_base::sync_with_stdio(false); cin>>n; cin>>str; 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; //if(pref_hash[i] > hashmod) pref_hash[i] -= 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; bool dwn = false, upp = false; vector<int>v; for(int i=1;i<=n;i++) { if(i == 1 && get_hash(2, 1+sz) == get_hash(2+sz, n)) { if(i <= mid) dwn = true; else upp = true; v.pb(i); } else if(i == n && get_hash(1, sz) == get_hash(sz+1, n-1)) {v.pb(i); if(i<=mid) dwn = true; else upp = true;} else if(i == mid && get_hash(1, sz) == get_hash(mid+1, n)){v.pb(i); if(i<=mid) dwn = true; else upp = true;} 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); if(i<=mid) dwn = true; else upp = true;} } 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(i<=mid) dwn = true; else upp = true;} } } if(v.size() == 0) { cout<<"NOT POSSIBLE\n"; return 0; } if(v.size() == 1) { for(int i:v) { if(i == 1) cout<<str.substr(mid, half_sz); //hashes.insert(get_hash(mid+1, n)); else if(i == n) cout<<str.substr(0, half_sz); //hashes.insert(get_hash(1, mid)); else if(i == mid) cout<<str.substr(mid, half_sz); //hashes.insert(get_hash(n-half_sz+1, n)); else if(i < mid) cout<<str.substr(mid, half_sz); //hashes.insert(get_hash(n-half_sz+1, n)); else if(i > mid) cout<<str.substr(0, half_sz); //hashes.insert(get_hash(1, mid)); return 0; } } if(upp && dwn && get_hash(1, mid-1) != get_hash(mid+1, n)) { cout<<"NOT UNIQUE\n"; } else { for(int i:v) { if(i == 1) cout<<str.substr(mid, half_sz); //hashes.insert(get_hash(mid+1, n)); else if(i == n) cout<<str.substr(0, half_sz); //hashes.insert(get_hash(1, mid)); else if(i == mid) cout<<str.substr(mid, half_sz); //hashes.insert(get_hash(n-half_sz+1, n)); else if(i < mid) cout<<str.substr(mid, half_sz); //hashes.insert(get_hash(n-half_sz+1, n)); else if(i > mid) cout<<str.substr(0, half_sz); //hashes.insert(get_hash(1, mid)); return 0; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...