-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path214. Shortest Palindrome.cpp
47 lines (43 loc) · 1.21 KB
/
214. Shortest Palindrome.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.
Example 1:
Input: "aacecaaa"
Output: "aaacecaaa"
Example 2:
Input: "abcd"
Output: "dcbabcd"
The first solution using Manacher algorithm
*/
class Solution {
public:
string shortestPalindrome(string s) {
int mx = 0, id = 0, resCen = 0, resLen = 0;
string temp = "$#";
for ( int i = 0; i < s.size(); i++ )
{
temp += s[i];
temp += '#';
}
vector<int> p( temp.size(), 0 );
for ( int i = 1; i < temp.size(); i++ )
{
p[i] = mx > i ? min( p[id * 2 - i], mx - i ) : 1;
while ( temp[i + p[i]] == temp[i - p[i]] ) ++p[i];
if ( mx < p[i] + i )
{
mx = p[i] + i;
id = i;
}
}
int pos;
for (int i = temp.size() - 2; i; i--) {
if ( i == p[i] ) {
pos = p[i] - 1;
break;
}
}
string tail = s.substr( pos );
reverse( tail.begin(), tail.end() );
return tail + s;
}
};