Skip to content

Commit b79a92f

Browse files
dp init #8
1 parent 8c702d1 commit b79a92f

File tree

4 files changed

+273
-0
lines changed

4 files changed

+273
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Problem Statement :
2+
Given a sequence, find the length of the longest palindromic subsequence in it.
3+
Approach: The idea is very straightforward. We are familiar with the approach of finding the longest possible common subsequence of two given strings. Here, we and utilize that concept on the given string and its reverse to obtain the longest possible palindromic subsequence.
4+
Therefore, the Longest Palindromic subsequence (LPS) is nothing but the Longest Common Subsequence problem on the input string s and reverse(s).
5+
[Note] LPS(a,n) = LCS(a,reverse(a),n);
6+
7+
Sample Test Case :
8+
3
9+
bbbab
10+
helloracecar
11+
agbcba
12+
case #1 : 4
13+
case #2 : 7
14+
case #3 : 5
15+
*/
16+
#include "headers.hpp"
17+
18+
int LCS(string, string, int);
19+
int LPS(string s, int n)
20+
{
21+
string rev = s;
22+
reverse(rev.begin(), rev.end());
23+
24+
return LCS(s, rev, n);
25+
}
26+
27+
int LCS(string a, string b, int n)
28+
{
29+
int dp[n + 1][n + 1];
30+
31+
for (int i = 0; i <= n; i++)
32+
dp[0][i] = 0;
33+
for (int i = 0; i <= n; i++)
34+
dp[i][0] = 0;
35+
36+
for (int i = 1; i <= n; i++)
37+
{
38+
for (int j = 1; j <= n; j++)
39+
{
40+
if (a[i - 1] == b[j - 1])
41+
dp[i][j] = 1 + dp[i - 1][j - 1];
42+
else
43+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
44+
}
45+
}
46+
return dp[n][n];
47+
}
48+
int main()
49+
{
50+
vi output;
51+
tests(t)
52+
{
53+
string s;
54+
cin >> s;
55+
int n = s.size();
56+
output.pb(LPS(s, n));
57+
}
58+
loop(i, output.size()) cout << "case #" << i + 1 << " : " << output[i] << endl;
59+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* Problem Statement: Minimum number of deletions to make a string palindrome
2+
Given a string of size ‘n’. The task is to remove or delete a minimum number of characters from the string so that the resultant string is a palindrome.
3+
4+
Sample Test Case :
5+
3
6+
aebcbda
7+
geeksforgeeks
8+
racecar
9+
case #1 : 2
10+
case #2 : 8
11+
case #3 : 0
12+
*/
13+
14+
#include "headers.hpp"
15+
int LCS(string, string, int);
16+
int minDeletionsToMakeStringPalindrome(string str, int n)
17+
{
18+
string revStr = str;
19+
reverse(revStr.begin(), revStr.end());
20+
return n - LCS(str, revStr, n);
21+
}
22+
23+
int LCS(string a, string b, int n)
24+
{
25+
int dp[n + 1][n + 1];
26+
for (int i = 0; i <= n; i++)
27+
dp[i][0] = 0;
28+
for (int i = 0; i <= n; i++)
29+
dp[0][i] = 0;
30+
31+
for (int i = 1; i <= n; i++)
32+
{
33+
for (int j = 1; j <= n; j++)
34+
{
35+
if (a[i - 1] == b[j - 1])
36+
dp[i][j] = (1 + dp[i - 1][j - 1]);
37+
else
38+
{
39+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
40+
}
41+
}
42+
}
43+
return dp[n][n];
44+
}
45+
int main()
46+
{
47+
vi output;
48+
tests(t)
49+
{
50+
string str;
51+
cin >> str;
52+
int n = str.size();
53+
output.pb(minDeletionsToMakeStringPalindrome(str, n));
54+
}
55+
56+
loop(i, output.size()) cout << "case #" << i + 1 << " : " << output[i] << endl;
57+
return 0;
58+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include "headers.hpp"
2+
/* Problem Statement : A follow up problem of the Shortest Common Supersequence Problem.
3+
We can output any p&c of the resultant string. For example, a = "geek", b = "eke" -> the shortest possible supersequence will be of length = 5. Any valid string with both a & b strings as subsequences and of SCS length is allowed. (eg: "geeke", "gekek", etc.)
4+
5+
Sample Test Case :
6+
3
7+
geek eke
8+
racecar aceit
9+
students tents
10+
case #1 : gekek
11+
case #2 : raceitcar
12+
case #3 : students
13+
*/
14+
int **LCS(string s1, string s2, int n, int m)
15+
{
16+
int **dp = new int *[n + 1];
17+
loop(i, n + 1)
18+
dp[i] = new int[m + 1];
19+
20+
for (int i = 0; i <= m; i++)
21+
dp[0][i] = 0;
22+
for (int i = 0; i <= n; i++)
23+
dp[i][0] = 0;
24+
25+
for (int i = 1; i <= n; i++)
26+
{
27+
for (int j = 1; j <= m; j++)
28+
{
29+
if (s1[i - 1] == s2[j - 1])
30+
dp[i][j] = (1 + dp[i - 1][j - 1]);
31+
else
32+
{
33+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
34+
}
35+
}
36+
}
37+
return dp;
38+
}
39+
string printShortestCommonSuperSequence(string s1, string s2, int n, int m)
40+
{
41+
int **dp = LCS(s1, s2, n, m);
42+
string output = "";
43+
int i = n, j = m;
44+
45+
while (i > 0 and j > 0)
46+
{
47+
if (s1[i - 1] == s2[j - 1])
48+
{
49+
output += (s1[i - 1]);
50+
i--;
51+
j--;
52+
}
53+
else
54+
{
55+
if (dp[i][j - 1] > dp[i - 1][j])
56+
{
57+
output += (s2[j - 1]);
58+
j--;
59+
}
60+
else
61+
{
62+
output += (s1[i - 1]);
63+
i--;
64+
}
65+
}
66+
}
67+
while (i > 0)
68+
{
69+
output += s1[i - 1];
70+
i--;
71+
}
72+
while (j > 0)
73+
{
74+
output += s2[j - 1];
75+
j--;
76+
}
77+
reverse(output.begin(), output.end()); //not needed here, as the length and the count of the resultant characters are preserved.
78+
return output;
79+
}
80+
81+
int main()
82+
{
83+
v(string) output;
84+
tests(t)
85+
{
86+
string s1, s2;
87+
cin >> s1 >> s2;
88+
int n = s1.size(), m = s2.size();
89+
output.pb(printShortestCommonSuperSequence(s1, s2, n, m));
90+
}
91+
loop(i, output.size()) cout << "case #" << i + 1 << " : " << output[i] << endl;
92+
return 0;
93+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* Problem Statement: We are given two strings, a and b. We need to write a function to check if string a exists as a subsequence of string b.
2+
3+
[Note] A subsequence of a string is a string where the order of the characters is preserved, but it can be at discontinuous positions. For example, If HelloWorld is our main string. A valid subsequence is Horld -> the order of characters is preserved, but the characters are allowed to be discontinuous.
4+
5+
Input a larger String (b), and then a smaller string (a). Check if the second string(a) exists as a subsequence of the first string (b)
6+
The approach in short: The idea here is very simple. We can check for the longest possible common subsequence between a and b. Since we know the first string is the larger one, the LCS length must correspond to the length of the smaller string (a) for it to be a valid subsequence present in string b.
7+
Thus, we obtain the length of the LCS(a,b), and then in the driver function, we return true if the length of LCS and string a is the same, else return false.
8+
9+
Sample Test Case :
10+
3
11+
DynamicProgramming DP
12+
HelloWorld Horld
13+
ABBEGYDH ABDY
14+
case #1 : 1
15+
case #2 : 1
16+
case #3 : 0
17+
*/
18+
19+
#include "headers.hpp"
20+
21+
int LCS(string &, string &, int, int);
22+
bool sequencePatternMatch(string &a, string &b)
23+
{
24+
int n = a.size(), m = b.size();
25+
int lcs = LCS(a, b, n, m);
26+
return (n == lcs);
27+
}
28+
int LCS(string &a, string &b, int n, int m)
29+
{
30+
int dp[n + 1][m + 1];
31+
32+
for (int i = 0; i <= m; i++)
33+
dp[0][i] = 0;
34+
for (int i = 0; i <= n; i++)
35+
dp[i][0] = 0;
36+
37+
for (int i = 1; i <= n; i++)
38+
{
39+
for (int j = 1; j <= m; j++)
40+
{
41+
if (a[i - 1] == b[j - 1])
42+
dp[i][j] = (1 + dp[i - 1][j - 1]);
43+
else
44+
{
45+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
46+
}
47+
}
48+
}
49+
return dp[n][m];
50+
}
51+
int main()
52+
{
53+
v(bool) output;
54+
tests(t)
55+
{
56+
string a, b;
57+
cin >> b >> a;
58+
output.pb(sequencePatternMatch(a, b));
59+
}
60+
61+
loop(i, output.size()) cout << "case #" << i + 1 << " : " << output[i] << endl;
62+
return 0;
63+
}

0 commit comments

Comments
 (0)