Skip to content

Commit

Permalink
feat: update lc-1392
Browse files Browse the repository at this point in the history
important! 0x3f3f3f3f is not prime.

Signed-off-by: Certseeds <[email protected]>
  • Loading branch information
Certseeds committed Sep 13, 2023
1 parent 1f59327 commit 36d6af1
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions algorithm/string/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ LIST(APPEND dependencies 14 22 38 344 535)
LIST(APPEND dependencies 657 387 383 so_05 242)
LIST(APPEND dependencies so_58 709 763 771 784)
LIST(APPEND dependencies 1324 1328 1358 1366 1370)
LIST(APPEND dependencies 1392)
LIST(TRANSFORM dependencies PREPEND leetcode_)

foreach (elementName IN LISTS dependencies)
Expand Down
54 changes: 54 additions & 0 deletions algorithm/string/leetcode_1392.cpp
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);
}

}
}
51 changes: 51 additions & 0 deletions algorithm/string/leetcode_1392_test.hpp
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

0 comments on commit 36d6af1

Please sign in to comment.