From 4ff735733669c11a6f132304d0a900c4cbcae7c3 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Sun, 19 Mar 2023 01:56:20 +0800 Subject: [PATCH] add: 230318-CN --- cpp/2303/230318-CN.cpp | 95 +++++++++++++++++++++++++++++++++++++++++ cpp/2303/CMakeLists.txt | 2 +- 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 cpp/2303/230318-CN.cpp diff --git a/cpp/2303/230318-CN.cpp b/cpp/2303/230318-CN.cpp new file mode 100644 index 0000000..7c8399a --- /dev/null +++ b/cpp/2303/230318-CN.cpp @@ -0,0 +1,95 @@ +#include + +/** + * 1616. Split Two Strings to Make Palindrome + * + * You are given two strings a and b of the same length. Choose an index and split both strings at the same index, splitting a into two strings: aprefix and asuffix where a = aprefix + asuffix, and splitting b into two strings: bprefix and bsuffix where b = bprefix + bsuffix. Check if aprefix + bsuffix or bprefix + asuffix forms a palindrome. + * When you split a string s into sprefix and ssuffix, either ssuffix or sprefix is allowed to be empty. For example, if s = "abc", then "" + "abc", "a" + "bc", "ab" + "c" , and "abc" + "" are valid splits. + * Return true if it is possible to form a palindrome string, otherwise return false. + * Notice that x + y denotes the concatenation of strings x and y. + */ + +class Solution { +public: + static bool checkPalindromeFormation(const std::string& a, const std::string& b) { + const int m = a.length(), n = b.length(); + if (m == 1 || n == 1) + return true; + + // prefix[shorter](b) + suffix[longer](a) + // prefix b, base a + if (n >= ((m >> 1) + (m & 1))) { + bool OK = true; + // prefix (i) operates on a first and optionally switches to b + // suffix (j) always operates on a + for (int i = (m >> 1) - !(m & 1), j = m >> 1, switched = 0; i >= 0; --i, ++j) { + if ((switched ? b : a)[i] == a[j]) + continue; + switched = 1; + if (b[i] == a[j]) + continue; + OK = false; + break; + } + if (OK) return true; + } + + // prefix(a) + suffix(b) + // prefix a, base b + if (m >= ((n >> 1) + (n & 1))) { + bool OK = true; + for (int i = (n >> 1) - !(n & 1), j = m >> 1, switched = 0; i >= 0; --i, ++j) { + if ((switched ? a : b)[i] == b[j]) + continue; + switched = 1; + if (a[i] == b[j]) + continue; + OK = false; + break; + } + if (OK) return true; + } + + // prefix a, base a + if (n >= m) { + bool OK = true; + for (int i = (m >> 1) - !(m & 1), j = m >> 1, switched = 0; i >= 0; --i, ++j) { + if (((switched |= (j >= m)) ? b : a)[j] == a[i]) + continue; + switched = 1; + if (a[i] == b[j]) + continue; + OK = false; + break; + } + if (OK) return true; + } + + // prefix b, base b + if (m >= n) { + bool OK = true; + for (int i = (n >> 1) - !(n & 1), j = m >> 1, switched = 0; i >= 0; --i, ++j) { + if (((switched |= (j >= n)) ? a : b)[j] == b[i]) + continue; + switched = 1; + if (b[i] == a[j]) + continue; + OK = false; + break; + } + if (OK) return true; + } + + // can NEVER reach here + return false; + } +}; + +int main() { + bool ans = Solution::checkPalindromeFormation( + "aejbaalflrmkswrydwdkdwdyrwskmrlfqizjezd", + "uvebspqckawkhbrtlqwblfwzfptanhiglaabjea" + ); + std::printf(ans ? "true" : "false"); + return 0; +} diff --git a/cpp/2303/CMakeLists.txt b/cpp/2303/CMakeLists.txt index 6994235..00758d0 100644 --- a/cpp/2303/CMakeLists.txt +++ b/cpp/2303/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2303) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2303 230317.cpp) +ADD_EXECUTABLE(2303 230318-CN.cpp)