-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path(Using Binary search)Longest Palindromic Substring
90 lines (82 loc) · 1.63 KB
/
(Using Binary search)Longest Palindromic 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
84
85
86
87
88
89
90
//Author: Fuadul Hasan([email protected])
//BSMRSTU,Gopalganj
#include<bits/stdc++.h>
using namespace std;int tc = 1;
#define happy ios::sync_with_stdio(false);
#define coding cin.tie(0);
#define pb push_back
#define mp make_pair
#define ll long long
#define pr pair<int, int>
#define vpr vector<pr>
#define vi std::vector<int>
#define vll std::vector<ll>
#define all(n) n.begin(),n.end()
#define Test printf("Case %d: ",tc++);
#define YES printf("YES\n");
#define NO printf("NO\n");
#define Yes printf("Yes\n");
#define No printf("No\n");
const int mod = 1e9 + 7;
const ll Inf = (ll)2e18 + 5;
const int N = 2e5 + 5;
bool is_paliondrom(string s){
string rev = s;
reverse(rev.begin(),rev.end());
return s==rev;
}
int good(int x,string s){
int n = s.length();
for(int l = 0;l+x<=n;l++){
if(is_paliondrom(s.substr(l,x))){
return l;
}
}
return -1;
}
string longestPalindrome(string s){
int best_len = 0;
string best_s = "";
int n = s.length();
for(int parity: {0,1}){
int low = 1,high = n;
if(low%2!=parity) low++;
if(high%2!=parity) high--;
cout<<low<<" "<<high<<" "<<parity<<endl;
while(low <= high){
int mid = (low + high)/2;
if(mid%2 != parity){
mid ++;
}
if(mid>high){
break;
}
cout<<mid<<endl;
int tmp = good(mid,s);
if(tmp != -1){
if(mid>best_len){
best_len = mid;
best_s = s.substr(tmp,mid);
}
low = mid + 2;
}else {
high = mid - 2;
}
}
}
return best_s;
}
int solve()
{
//happy coding
string s;
cin>>s;
string x = longestPalindrome(s);
cout<<x<<endl;
return 0;
}
int main(){
int test = 1;
scanf("%d", &test);
while (test--)solve();return 0;
}