-
Notifications
You must be signed in to change notification settings - Fork 985
/
Copy pathPrintShortestCommonSupersequence.cpp
93 lines (78 loc) · 1.78 KB
/
PrintShortestCommonSupersequence.cpp
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
91
92
93
//Print Shortest Common Supersequence
#include<bits/stdc++.h>
using namespace std;
class Solution
{
public:
//Function to find shortest common supersequence string of two strings.
string shortestCommonSupersequence(string x, string y, int m, int n)
{
int dp[m+1][n+1];
for(int i=0; i<=m; i++)
{
for(int j=0; j<=n; j++)
{
if(i == 0 || j == 0)
{
dp[i][j] = 0;
}
else if(x[i-1] == y[j-1])
{
dp[i][j] = dp[i-1][j-1] + 1;
}
else
{
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
}
int i = m, j = n;
string ans = "";
while(i > 0 && j > 0)
{
if(x[i-1] == y[j-1])
{
ans.push_back(x[i-1]);
i--;
j--;
}
else if(dp[i][j-1] > dp[i-1][j])
{
ans.push_back(y[j-1]);
j--;
}
else
{
ans.push_back(x[i-1]);
i--;
}
}
while(i>0)
{
ans.push_back(x[i-1]);
i--;
}
while(j>0)
{
ans.push_back(y[j-1]);
j--;
}
reverse(ans.begin(), ans.end());
return ans;
}
};
int main()
{
int t;
//taking total testcases
cin >> t;
while(t--){
string X, Y;
//taking String X and Y
cin >> X >> Y;
//calling function shortestCommonSupersequence()
Solution obj;
cout << obj.shortestCommonSupersequence(X, Y, X.size(), Y.size())<< endl;
}
return 0;
}