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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <string>
#include <iostream>
using namespace std;
string longestPalindrome(string s)
{
// Start typing your C/C++ solution below
// DO NOT write int main() function
int size = s.size();
char *str = new char[size*2+3];
str[0] = '$';
str[1] = '#';
str[size*2+2] = 0;
for(int i = 0; i < size; ++i)
{
str[2*i+2] = s[i];
str[2*i+3] = '#';
}
int *p = new int[size*2+2];
memset(p, 0, sizeof(int)*(size*2+2));
p[0] = 1;
int mid = 0;
int mx = 1;
int maxi = 0;
int maxlen = 1;
for(int i = 1; str[i]; ++i)
{
if(mx > i)
p[i] = min(p[2*mid-i], mx-i);
else
p[i] = 1;
while(str[i-p[i]] == str[i+p[i]])
p[i]++;
if(mx < i+p[i])
{
mx = i + p[i];
mid = i;
}
if(maxlen < p[i])
{
maxlen = p[i];
maxi = i;
}
}
delete str;
delete p;
return s.substr((maxi-maxlen+1)/2, maxlen-1);
}
int main()
{
string s;
while(true)
{
cin >> s;
cout << longestPalindrome(s) << '\n';
}
}
Previous Next