forked from amantyagi22/dynamic-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalidromicQueries.cpp
More file actions
52 lines (50 loc) · 1.09 KB
/
palidromicQueries.cpp
File metadata and controls
52 lines (50 loc) · 1.09 KB
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
#include<iostream>
using namespace std;
/* 0 --> true, 1 --> false */
//Top Down approach
int pldString(string s,int l,int r,int dp[][100]){
if(l>r){
return 0;
}
int ans;
if(dp[l][r]!=0){
return dp[l][r];
}
if(s[l]!=s[r]){
ans = 1;
}else{
ans = pldString(s,l+1,r-1,dp);
}
return dp[l][r] = ans;
}
//Bottom Up Approach
int pldStringdp(string s,int l,int r){
int dp[100][100] = {0};
for(int i=0;i<=l;i++){
for(int j=0;j<=r;j++){
if(s[i]==s[j]){
dp[i][j] = dp[i+1][j-1];
}else{
dp[i][j] = 1;
}
}
}
return dp[l][r];
}
//Space optimized solution
//Kadane's algorithm
int main(){
int n; cin >> n;
string s; cin >> s;
int q; cin >> q;
while(q--){
int l,r; cin >> l >> r;
int dp[100][100] = {0};
if(!pldString(s,l-1,r-1,dp)){
cout << "YES" << endl;
}else cout << "NO" << endl;
if(!pldStringdp(s,l-1,r-1)){
cout << "YES" << endl;
}else cout << "NO" << endl;
}
}