add: 220525-CN [cpp]

This commit is contained in:
Eat-Swap 2022-05-26 20:29:05 +08:00
parent 0e8f9b9065
commit 86c7a0bb11
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 20 additions and 1 deletions

19
cpp/2205/220525-CN.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <string>
#include <numeric>
/**
* 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);
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2205)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2205 220525.cpp) ADD_EXECUTABLE(2205 220525-CN.cpp)