-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path45.cpp
More file actions
39 lines (37 loc) · 839 Bytes
/
45.cpp
File metadata and controls
39 lines (37 loc) · 839 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
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main(){
//program to find longest plaindromic substring in O(n2) time.
string s;
cin>>s;
int si = 0;
int m = 1;
int low = 0, hi = 0;
for(int i=1;i<s.length();i++){
low = i-1;
hi = i;
while(low>=0 && hi<s.length() && s[low]==s[hi]){
if(hi - low + 1 > m){
m = hi - low + 1;
si = low;
}
low--;
hi--;
}
low = i-1;
hi = i+1;
while(low>=0 && hi<s.length() && s[low]==s[hi]){
if(hi - low + 1 > m){
m = hi - low + 1;
si = low;
}
low--;
hi--;
}
}
for(int i=si;i<si+m-1;i++){
cout<<s[i];
}
return 0;
}