-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp.cpp
More file actions
45 lines (39 loc) · 991 Bytes
/
p.cpp
File metadata and controls
45 lines (39 loc) · 991 Bytes
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
#include<bits/stdc++.h>
using namespace std;
class Solution{
public:
int center (string s, int left, int right){
int size = s.length();
while(left >= 0 && right < size && s[left] == s[right]){
left-=1;
right+=1;
}
return right - left - 1;
}
string longestPalin(string s){
int size = s.length();
if(size<=1) return s;
int start = 0;
int maxLength = 1;
for(auto& x : s){
int length1 = center(s,x,x);
int length2 = center(s,x,x+1);
int length = max(length1,length2);
if(length>maxLength){
start = x - (length -1)/2;
maxLength = length;
}
}
return s.substr(start,maxLength);
}
};
int main(){
string s;
getline(cin,s);
Solution solution;
string result = solution.longestPalin(s);
for(auto& i : result){
cout<<i<<" ";
}
return 0;
}