Skip to content

Commit f578c28

Browse files
dp init #9
dp init #9
2 parents 6032808 + 18ad6c0 commit f578c28

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Optimised Solution of Matrix Chain Multiplication Problem using the Bottom Up Dynamic Programming.
3+
Sample Test Case :
4+
3
5+
3
6+
10 20 30
7+
5
8+
40 20 30 10 30
9+
4
10+
10 30 5 60
11+
case #1 : 6000
12+
case #2 : 26000
13+
case #3 : 4500
14+
*/
15+
#include "../headers.hpp"
16+
17+
int matrixChainMultiplicationCost(int *arr, int i, int j, int *dp[])
18+
{
19+
if (i >= j)
20+
return dp[i][j] = 0;
21+
if (dp[i][j] != -1)
22+
return dp[i][j];
23+
24+
int ans = INT_MAX;
25+
for (int k = i; k <= j - 1; k++)
26+
{
27+
int temp_ans = matrixChainMultiplicationCost(arr, i, k, dp) + matrixChainMultiplicationCost(arr, k + 1, j, dp) + (arr[i - 1] * arr[k] * arr[j]);
28+
if (ans > temp_ans)
29+
ans = temp_ans;
30+
}
31+
return dp[i][j] = ans;
32+
}
33+
int main()
34+
{
35+
vi output;
36+
tests(t)
37+
{
38+
int n;
39+
cin >> n;
40+
int *arr = new int[n];
41+
loop(i, n) cin >> arr[i];
42+
int **dp = new int *[n + 1];
43+
loop(i, n + 1)
44+
{
45+
dp[i] = new int[n + 1];
46+
}
47+
// memset(dp, -1, sizeof(dp)); did not work on system compiler, hence manually assigned -1 to dp[][].
48+
loop(i, n + 1)
49+
{
50+
loop(j, n + 1)
51+
dp[i][j] = -1;
52+
}
53+
output.pb(matrixChainMultiplicationCost(arr, 1, n - 1, dp));
54+
}
55+
loop(i, output.size()) cout << "case #" << i + 1 << " : " << output[i] << endl;
56+
return 0;
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* Problem Statement : Matrix Chain Multiplication
2+
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The problem is not actually to perform the multiplications, but merely to decide in which order to perform the multiplications.
3+
We have many options to multiply a chain of matrices because matrix multiplication is associative. In other words, no matter how we parenthesize the product, the result will be the same.
4+
Sample Test Case :
5+
3
6+
3
7+
10 20 30
8+
5
9+
40 20 30 10 30
10+
4
11+
10 30 5 60
12+
case #1 : 6000
13+
case #2 : 26000
14+
case #3 : 4500
15+
*/
16+
#include "../headers.hpp"
17+
18+
int matrixChainMultiplicationCost(int *arr, int i, int j)
19+
{
20+
if (i >= j)
21+
return 0;
22+
23+
int ans = INT_MAX;
24+
for (int k = i; k <= j - 1; k++)
25+
{
26+
int temp_ans = matrixChainMultiplicationCost(arr, i, k) + matrixChainMultiplicationCost(arr, k + 1, j) + (arr[i - 1] * arr[k] * arr[j]);
27+
if (ans > temp_ans)
28+
ans = temp_ans;
29+
}
30+
return ans;
31+
}
32+
int main()
33+
{
34+
vi output;
35+
tests(t)
36+
{
37+
int n;
38+
cin >> n;
39+
int *arr = new int[n];
40+
loop(i, n) cin >> arr[i];
41+
output.pb(matrixChainMultiplicationCost(arr, 1, n - 1));
42+
}
43+
loop(i, output.size()) cout << "case #" << i + 1 << " : " << output[i] << endl;
44+
return 0;
45+
}

DynamicProgramming/longestPalindromicSubsequence.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,5 @@ int main()
5656
output.pb(LPS(s, n));
5757
}
5858
loop(i, output.size()) cout << "case #" << i + 1 << " : " << output[i] << endl;
59+
return 0;
5960
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "headers.hpp"
2+
/* Problem Statement: Given a string, print the longest repeating subsequence such that the two subsequences don’t have the same string character at the same position, i.e., any i’th character in the two subsequences shouldn’t have the same index in the original string.
3+
The two subsequences are 'a'(first) and 'a' (second). Note that 'b' cannot be considered as part of subsequence as it would be at the same
4+
index in both.
5+
6+
Sample Test Case :
7+
3
8+
AABEBCDD
9+
ABCDDBC
10+
eloeggogll
11+
case #1 : 3
12+
case #2 : 2
13+
case #3 : 4
14+
*/
15+
int longestRepeatingSubsequence(string s1, int n)
16+
{
17+
string s2 = s1;
18+
19+
int dp[n + 1][n + 1];
20+
for (int i = 0; i <= n; i++)
21+
dp[0][i] = dp[i][0] = 0;
22+
23+
for (int i = 1; i <= n; i++)
24+
{
25+
for (int j = 1; j <= n; j++)
26+
{
27+
if (s1[i - 1] == s2[j - 1] and (i != j))
28+
dp[i][j] = 1 + dp[i - 1][j - 1];
29+
else
30+
{
31+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
32+
}
33+
}
34+
}
35+
return dp[n][n];
36+
}
37+
int main()
38+
{
39+
vi output;
40+
tests(t)
41+
{
42+
string s;
43+
cin >> s;
44+
int n = s.size();
45+
output.pb(longestRepeatingSubsequence(s, n));
46+
}
47+
loop(i, output.size()) cout << "case #" << i + 1 << " : " << output[i] << endl;
48+
return 0;
49+
}

0 commit comments

Comments
 (0)