From 86c7a0bb11bde420ef83f4b6f12dc460b475ce8f Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Thu, 26 May 2022 20:29:05 +0800 Subject: [PATCH] add: 220525-CN [cpp] --- cpp/2205/220525-CN.cpp | 19 +++++++++++++++++++ cpp/2205/CMakeLists.txt | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 cpp/2205/220525-CN.cpp diff --git a/cpp/2205/220525-CN.cpp b/cpp/2205/220525-CN.cpp new file mode 100644 index 0000000..9f435fc --- /dev/null +++ b/cpp/2205/220525-CN.cpp @@ -0,0 +1,19 @@ +#include +#include + +/** + * 467. Unique Substrings in Wraparound String + * We define the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: + * "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....". + * Given a string p, return the number of unique non-empty substrings of p are present in s. + */ + +class Solution { +public: + static int findSubstringInWraproundString(const std::string& p) { + int dp[26]{}, k = 0; + for (int i = 0; i < p.length(); ++i) + dp[p[i] - 'a'] = std::max(dp[p[i] - 'a'], k = 1 + ((i && (p[i] - p[i - 1] + 26) % 26 == 1) ? k : 0)); + return std::accumulate(dp, dp + 26, 0); + } +}; diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index f7df0ea..d0312df 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2205 220525.cpp) +ADD_EXECUTABLE(2205 220525-CN.cpp)