From 3d804ccdd70d25ad9080a376d2f2c6b63844bbe2 Mon Sep 17 00:00:00 2001 From: kamyu Date: Mon, 1 Feb 2021 11:09:58 +0800 Subject: [PATCH] Update palindrome-partitioning-iv.cpp --- C++/palindrome-partitioning-iv.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/C++/palindrome-partitioning-iv.cpp b/C++/palindrome-partitioning-iv.cpp index 50a56c7359..73565f9934 100644 --- a/C++/palindrome-partitioning-iv.cpp +++ b/C++/palindrome-partitioning-iv.cpp @@ -1,4 +1,4 @@ -// Time: O(n) +// Time: O(n^2) // Space: O(n) class Solution { @@ -12,28 +12,34 @@ class Solution { string T = preProcess(s); const int n = size(T); vector P(n); - vector dp(n); // dp[i]: s[:i] is composed of dp[i] palindromic strings + vector prefix, suffix; int C = 0, R = 0; for (int i = 1; i < n - 1; ++i) { int i_mirror = 2 * C - i; P[i] = (R > i) ? min(R - i, P[i_mirror]) : 0; while (T[i + 1 + P[i]] == T[i - 1 - P[i]]) { - if (dp[i - 1 - P[i]]) { - dp[(i + 1 + P[i]) + 1] = dp[i - 1 - P[i]] + 1; - } ++P[i]; } - if (i + 1 + P[i] == n - 1 && dp[(i - 1 - P[i]) + 1] == 2) { - return true; + if (i + 1 + P[i] == n - 1) { + suffix.emplace_back(i); } if (i - 1 - P[i] == 0) { - dp[i + 1 + P[i]] = 1; + prefix.emplace_back(i); } if (i + P[i] > R) { C = i; R = i + P[i]; } } + for (const auto& i : prefix) { + for (const auto& j : suffix) { + int left = i + 1 + P[i], right = j - 1 - P[j]; + int mid = left + (right - left) / 2; + if (P[mid] >= mid - left) { + return true; + } + } + } return false; }