Submission #58873

#TimeUsernameProblemLanguageResultExecution timeMemory
58873SpeedOfMagicThree Friends (BOI14_friends)C++17
0 / 100
209 ms53888 KiB
/** MIT License Copyright (c) 2018 Vasilyev Daniil **/ #include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") template<typename T> using v = vector<T>; #define int long long typedef string str; typedef vector<int> vint; #define rep(a, l, r) for(int a = (l); a < (r); a++) #define pb push_back #define sz(a) ((int) a.size()) const long long inf = 4611686018427387903; //2^62 - 1 #if 0 //FileIO const string fileName = ""; ifstream fin ((fileName == "" ? "input.txt" : fileName + ".in" )); ofstream fout((fileName == "" ? "output.txt" : fileName + ".out")); #define get fin>> #define put fout<< #else #define get cin>> #define put cout<< #endif #define eol put endl void read() {} template<typename Arg,typename... Args> void read (Arg& arg,Args&... args){get (arg) ;read(args...) ;} void print(){} template<typename Arg,typename... Args> void print(Arg arg,Args... args){put (arg)<<" ";print(args...);} void debug(){eol;} template<typename Arg,typename... Args> void debug(Arg arg,Args... args){put (arg)<<" ";debug(args...);} int getInt(){int a; get a; return a;} //code goes here const long long mod2 = 2000001881; const long long base = 29; const char startingLetter = 'A'; //starting letter of the alphabet long long hashChar(char c){ return c - startingLetter + 1; } long long hashStr(long long prevHash, char curChar, long long mod = mod2) { return ((prevHash * base) % mod + hashChar(curChar)) % mod; } long long hashStr(string& s) { long long res = 0; for(char i : s) res = hashStr(res, i); return res; } const int mod = mod2; const int N = 2e6 + 1; int powOfBase[N], invPowOfBase[N]; void run() { int n; get n; str u; get u; powOfBase[0] = 1; rep(i, 1, n + 1) powOfBase[i] = (base * powOfBase[i - 1]) % mod; str left = "", right = ""; rep(i, 0, n / 2 + 1) left += u[i]; rep(i, n / 2 + 1, n) right += u[i]; vint ans; rep(z, 0, 2) { vint hshLeft, hshRight; hshLeft.pb(hashChar(left[0])); rep(i, 1, sz(left)) { int d = hshLeft.back(); hshLeft.pb(hashStr(d, left[i])); } hshRight.pb(hashChar(right[0])); rep(i, 1, sz(right)) { int d = hshRight.back(); hshRight.pb(hashStr(d, right[i])); } if (z) { //debug(sz(hshRight), right, sz(hshLeft), left); rep(i, 0, sz(right)) { int resultHash = hshRight.back(); int offset = sz(right) - (i + 1); resultHash = ((resultHash - (powOfBase[offset] * hshRight[i]) % mod) % mod + mod) % mod; if (i) resultHash = (resultHash + (powOfBase[offset] * hshRight[i - 1]) % mod) % mod; if (resultHash == hshLeft.back()) ans.pb(i + sz(left)); } } else { rep(i, 0, sz(left)) { int resultHash = hshLeft.back(); int offset = sz(left) - (i + 1); resultHash = ((resultHash - (powOfBase[offset] * hshLeft[i]) % mod) % mod + mod) % mod; if (i) resultHash = (resultHash + (powOfBase[offset] * hshLeft[i - 1]) % mod) % mod; //debug(i, resultHash, hshRight.back()); if (resultHash == hshRight.back()) ans.pb(i); } } if (z) break; right = left.back() + right; left.pop_back(); } //debug(ans[0]); if (sz(ans) == 0) put "NOT POSSIBLE"; else if (sz(ans) == 1) { rep(i, 0, n / 2) if (i != ans[0]) put u[i]; else n++; } else put "NOT UNIQUE"; } int32_t main() {srand(time(0)); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); put fixed; put setprecision(15); run(); return 0;}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...