Submission #968751

#TimeUsernameProblemLanguageResultExecution timeMemory
968751youssef_3breheemThree Friends (BOI14_friends)C++17
100 / 100
162 ms87168 KiB
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <iomanip> using namespace std; typedef long long ll; typedef string str; typedef vector<ll> vint; const ll MOD1 = 1999998893; const ll MOD2 = 2100003611; const ll BASE = 29; const char STARTING_LETTER = 'A'; ll hashChar(char c) { return c - STARTING_LETTER + 1; } pair<ll, ll> hashStr(pair<ll, ll> prevHash, char curChar) { return {((prevHash.first * BASE) % MOD1 + hashChar(curChar)) % MOD1, ((prevHash.second * BASE) % MOD2 + hashChar(curChar)) % MOD2}; } void calculatePowers(ll powOfBase1[], ll powOfBase2[], int n) { powOfBase1[0] = 1; powOfBase2[0] = 1; for (int i = 1; i < n; i++) { powOfBase1[i] = (BASE * powOfBase1[i - 1]) % MOD1; powOfBase2[i] = (BASE * powOfBase2[i - 1]) % MOD2; } } str findUniqueString(int n, const str& u) { str left = u.substr(0, n / 2 + 1); str right = u.substr(n / 2 + 1); str ans = ""; pair<ll, ll> ansHsh = {0, 0}; ll powOfBase1[n], powOfBase2[n]; calculatePowers(powOfBase1, powOfBase2, n); for (int z = 0; z < 2; z++) { vector<pair<ll, ll>> hshLeft, hshRight; hshLeft.push_back({hashChar(left[0]), hashChar(left[0])}); for (char c : left.substr(1)) { auto d = hshLeft.back(); hshLeft.push_back(hashStr(d, c)); } hshRight.push_back({hashChar(right[0]), hashChar(right[0])}); for (char c : right.substr(1)) { auto d = hshRight.back(); hshRight.push_back(hashStr(d, c)); } if (z) { for (int i = 1; i < right.size(); i++) { auto resultHash = hshRight.back(); int offset = right.size() - (i + 1); resultHash.first = ((resultHash.first - (powOfBase1[offset] * hshRight[i].first) % MOD1) % MOD1 + MOD1) % MOD1; if (i) resultHash.first = (resultHash.first + (powOfBase1[offset] * hshRight[i - 1].first) % MOD1) % MOD1; resultHash.second = ((resultHash.second - (powOfBase2[offset] * hshRight[i].second) % MOD2) % MOD2 + MOD2) % MOD2; if (i) resultHash.second = (resultHash.second + (powOfBase2[offset] * hshRight[i - 1].second) % MOD2) % MOD2; if (resultHash == hshLeft.back()) { if (ans.empty()) { for (int j = 0; j < right.size(); j++) if (i != j) ans += right[j]; for (char c : ans) ansHsh = hashStr(ansHsh, c); } else { if (resultHash != ansHsh) return "NOT UNIQUE"; } } } } else { for (int i = 0; i < left.size(); i++) { auto resultHash = hshLeft.back(); int offset = left.size() - (i + 1); resultHash.first = ((resultHash.first - (powOfBase1[offset] * hshLeft[i].first) % MOD1) % MOD1 + MOD1) % MOD1; if (i) resultHash.first = (resultHash.first + (powOfBase1[offset] * hshLeft[i - 1].first) % MOD1) % MOD1; resultHash.second = ((resultHash.second - (powOfBase2[offset] * hshLeft[i].second) % MOD2) % MOD2 + MOD2) % MOD2; if (i) resultHash.second = (resultHash.second + (powOfBase2[offset] * hshLeft[i - 1].second) % MOD2) % MOD2; if (resultHash == hshRight.back()) { if (ans.empty()) { for (int j = 0; j < left.size(); j++) if (i != j) ans += left[j]; for (char c : ans) ansHsh = hashStr(ansHsh, c); } else { if (resultHash != ansHsh) return "NOT UNIQUE"; } } } } if (z) break; right = left.back() + right; left.pop_back(); } if (ans.empty()) return "NOT POSSIBLE"; else return ans; } int main() { srand(time(0)); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(15); int n; cin >> n; str u; cin >> u; str result = findUniqueString(n, u); cout << result; return 0; }

Compilation message (stderr)

friends.cpp: In function 'str findUniqueString(int, const str&)':
friends.cpp:61:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |             for (int i = 1; i < right.size(); i++) {
      |                             ~~^~~~~~~~~~~~~~
friends.cpp:72:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |                         for (int j = 0; j < right.size(); j++)
      |                                         ~~^~~~~~~~~~~~~~
friends.cpp:84:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |             for (int i = 0; i < left.size(); i++) {
      |                             ~~^~~~~~~~~~~~~
friends.cpp:95:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |                         for (int j = 0; j < left.size(); j++)
      |                                         ~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...