std::string
manacher(
conststd::string &prototype) {
42 if(prototype.size() > 0) {
45std::string stuffed_string =
"";
46 for(
autostr : prototype) {
47stuffed_string += str;
48stuffed_string +=
"#";
50stuffed_string =
"@#"+ stuffed_string +
"&";
52std::vector<uint64_t> palindrome_max_half_length(
53stuffed_string.size(),
59uint64_t bigger_center =
69 for(uint64_t i = 1; i < stuffed_string.size() - 1; i++) {
72uint64_t opposite_to_i =
81palindrome_max_half_length[i] = std::min(
82palindrome_max_half_length[opposite_to_i], right - i);
87 while(stuffed_string[i + (palindrome_max_half_length[i] + 1)] ==
88stuffed_string[i - (palindrome_max_half_length[i] + 1)]) {
89palindrome_max_half_length[i]++;
96 if(i + palindrome_max_half_length[i] > right) {
98right = i + palindrome_max_half_length[i];
103uint64_t half_length = 0;
104uint64_t center_index = 0;
106 for(uint64_t i = 1; i < stuffed_string.size() - 1; i++) {
107 if(palindrome_max_half_length[i] > half_length) {
108half_length = palindrome_max_half_length[i];
113std::string palindromic_substring =
116 if(half_length > 0) {
122center_index - half_length +
125center_index + half_length -
127 for(uint64_t index = start; index <= end; index += 2) {
128palindromic_substring += stuffed_string[index];
134palindromic_substring = prototype[0];
136 returnpalindromic_substring;
165std::cout <<
"All tests have passed!"<< std::endl;
std::string manacher(const std::string &prototype)
A function that implements Manacher's algorithm.
static void test()
Self-test implementations.
int main()
Main function.
Functions for Manacher's Algorithm implementation.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4