-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathLongest palindrome substring
83 lines (69 loc) · 1.64 KB
/
Longest palindrome substring
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Solution {
public:
string longestPalindrome(string s) {
int ans=0;
int start=-1;
int n=s.size();
if(n<2)
return s;
for(int i=0;i<n-1;i++)
{
int p=1;
int ts;
int st=i-1,e=i+1;
while(st>=0&&e<n)
{
if(s[st]==s[e])
{
ts=st;
p+=2;
if(ans<p)
{
ans=p;
start=ts;
}
st--;
e++;
}
else{
break;
}
}
st=i;
e=i+1;
p=0;
while(st>=0&&e<n)
{
if(s[st]==s[e])
{
ts=st;
p+=2;
if(ans<p)
{
ans=p;
start=ts;
}
st--;
e++;
}
else{
break;
}
}
// cout<<p<<endl;
}
string out;
if(start==-1)
{
out +=s[0];
return out;
}
cout<<ans<<" "<<start<<endl;
for(int i=start;i<ans+start;i++)
{
out += s[i];
}
cout<<out<<endl;
return out;
}
};