-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
important! 0x3f3f3f3f is not prime. Signed-off-by: Certseeds <[email protected]>
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2023 nanoseeds | ||
*/ | ||
#include "leetcode_1392_test.hpp" | ||
#include <cstring> | ||
|
||
namespace leetcode_1392 { | ||
namespace leetcode_1392 { | ||
|
||
namespace naive { | ||
|
||
string longestPrefix(const string &s) { | ||
int32_t count{0}; | ||
for (int32_t i{0}; i < s.size() - 1; i++) { | ||
if (0 == std::memcmp(&s[0], &s[s.size() - 1 - i], (i + 1))) { | ||
count = i + 1; | ||
} | ||
} | ||
return s.substr(0, count); | ||
} | ||
|
||
} | ||
namespace hash { | ||
string longestPrefix(const string &s) { | ||
int32_t count{0}; | ||
constexpr const auto prime{1000'000'007},prime2{1000'000'009}; | ||
size_t hashPrefix{0}, hashPostfix{0}, hashMultiply{1}; | ||
size_t hashPrefix2{0}, hashPostfix2{0}, hashMultiply2{1}; | ||
for (int32_t i{0}; i < s.size() - 1; i++) { | ||
hashPrefix = (hashPrefix * 26 + s[i]) % prime; | ||
hashPrefix2 = (hashPrefix2 * 26 + s[i]) % prime2; | ||
hashPostfix = (hashPostfix + (s[s.size() - 1 - i]) * hashMultiply) % prime; | ||
hashPostfix2 = (hashPostfix2 + (s[s.size() - 1 - i]) * hashMultiply2) % prime2; | ||
hashMultiply = (hashMultiply * 26) % prime; | ||
hashMultiply2 = (hashMultiply2 * 26) % prime2; | ||
if (hashPrefix == hashPostfix && hashPrefix2 == hashPostfix2) { // 双哈希 | ||
count = i + 1; | ||
} | ||
} | ||
return s.substr(0, count); | ||
} | ||
} | ||
|
||
string longestPrefix(const string &s) { | ||
return hash::longestPrefix(s); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2023 nanoseeds | ||
*/ | ||
//@Tag string | ||
//@Tag 字符串 | ||
|
||
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_STRING_LEETCODE_1392_TEST_HPP | ||
#define CS203_DSAA_TEMPLATE_ALGORITHM_STRING_LEETCODE_1392_TEST_HPP | ||
|
||
#include <catch_main.hpp> | ||
#include <cstdint> | ||
#include <cstddef> | ||
#include <string> | ||
|
||
namespace leetcode_1392 { | ||
using std::string; | ||
|
||
namespace leetcode_1392 { | ||
string longestPrefix(const string &s); | ||
} | ||
|
||
TEST_CASE("1 [test_1392]", "[test_1392]") { | ||
constexpr const char *const input{"abababab"}; | ||
constexpr const char *const result{"ababab"}; | ||
CHECK(result == leetcode_1392::longestPrefix(input)); | ||
} | ||
|
||
TEST_CASE("2 [test_1392]", "[test_1392]") { | ||
constexpr const char *const input{"longestPrefix"}; | ||
constexpr const char *const result{""}; | ||
CHECK(result == leetcode_1392::longestPrefix(input)); | ||
} | ||
|
||
TEST_CASE("3 [test_1392]", "[test_1392]") { | ||
const string input(98001, 'a'); | ||
const string result(98000, 'a'); | ||
CHECK(result == leetcode_1392::longestPrefix(input)); | ||
} | ||
TEST_CASE("4 [test_1392]", "[test_1392]") { | ||
constexpr const char *const input{"vwantmbocxcwrqtvgzuvgrmdltfiglltaxkjfajxthcppcatddcunpkqsgpnjjgqanrwabgrtwuqbrfl"}; | ||
constexpr const char *const result{""}; | ||
CHECK(result == leetcode_1392::longestPrefix(input)); | ||
} | ||
|
||
|
||
} | ||
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_STRING_LEETCODE_1392_TEST_HPP |