add: 220525-CN [cpp]
This commit is contained in:
parent
0e8f9b9065
commit
86c7a0bb11
|
|
@ -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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue