Last active
December 23, 2015 04:39
-
-
Save stevenRush/6582028 to your computer and use it in GitHub Desktop.
1354. Palindrome. Again Palindrome
This file contains hidden or 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
#include <vector> | |
#include <string> | |
#include <iostream> | |
#include <algorithm> | |
void z_function (const std::string & s, std::vector<int> & z) | |
{ | |
size_t length = s.length(); | |
z.clear(); | |
z.resize(length); | |
for (int i = 1, l = 0, r = 0; i < length; ++i) | |
{ | |
if (i <= r) | |
z[i] = std::min (r-i+1, z[i-l]); | |
while (i+z[i] < length && s[z[i]] == s[i+z[i]]) | |
++z[i]; | |
if (i+z[i]-1 > r) | |
l = i, r = i+z[i]-1; | |
} | |
} | |
size_t find_suff_length(const std::vector<int> & z) | |
{ | |
size_t string_length = (z.size() - 1) / 2; | |
for(auto it = z.begin() + string_length + 2; it != z.end(); ++it) | |
{ | |
if (*it == std::distance(it, z.end())) | |
return *it; | |
} | |
return 0; | |
} | |
int main() | |
{ | |
//freopen("C:\\temp\\input.txt", "r", stdin); | |
std::vector<int> pi; | |
std::string st; | |
std::cin >> st; | |
std::string reversed_st = st; | |
std::reverse(reversed_st.begin(), reversed_st.end()); | |
std::string final_string = reversed_st + "#" + st; | |
std::vector<int> z; | |
z_function(final_string, z); | |
auto max = find_suff_length(z); | |
std::string answer = st + reversed_st.substr(max, st.length() - max); | |
std::cout << answer << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment