TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
shortest_common_supersequence.cpp
Go to the documentation of this file.
1
17// header files
18#include <iostream>
19#include <string>
20#include <vector>
21#include <algorithm>
22#include <cassert>
23
28namespace dynamic_programming {
29
35
42 std::string scs(const std::string &str1, const std::string &str2) {
43
44 // Edge cases
45 // If either str1 or str2 or both are empty
46 if(str1.empty() && str2.empty()) {
47 return "";
48 }
49 else if(str1.empty()) {
50 return str2;
51 }
52 else if(str2.empty()) {
53 return str1;
54 }
55
56 // creating lookup table
57 std::vector <std::vector <int>> lookup(str1.length() + 1, std::vector <int> (str2.length() + 1, 0));
58
59 for(int i=1; i <= str1.length(); i++) {
60 for(int j=1; j <= str2.length(); j++) {
61 if(str1[i-1] == str2[j-1]) {
62 lookup[i][j] = lookup[i-1][j-1] + 1;
63 }
64 else {
65 lookup[i][j] = std::max(lookup[i-1][j], lookup[i][j-1]);
66 }
67 }
68 }
69
70 // making supersequence
71 // i and j are initially pointed towards end of strings
72 // Super-sequence will be constructed backwards
73 int i=str1.length();
74 int j=str2.length();
75 std::string s;
76
77 while(i>0 && j>0) {
78
79 // If the characters at i and j of both strings are same
80 // We only need to add them once in s
81 if(str1[i-1] == str2[j-1]) {
82 s.push_back(str1[i-1]);
83 i--;
84 j--;
85 }
86 // otherwise we check lookup table for recurrences of characters
87 else {
88 if(lookup[i-1][j] > lookup[i][j-1]) {
89 s.push_back(str1[i-1]);
90 i--;
91 }
92 else {
93 s.push_back(str2[j-1]);
94 j--;
95 }
96 }
97 }
98
99 // copying remaining elements
100 // if j becomes 0 before i
101 while(i > 0) {
102 s.push_back(str1[i-1]);
103 i--;
104 }
105
106 // if i becomes 0 before j
107 while(j > 0) {
108 s.push_back(str2[j-1]);
109 j--;
110 }
111
112 // As the super sequence is constructd backwards
113 // reversing the string before returning gives us the correct output
114 reverse(s.begin(), s.end());
115 return s;
116 }
117 } // namespace shortest_common_supersequence
118} // namespace dynamic_programming
119
124static void test() {
125 // custom input vector
126 std::vector <std::vector <std::string>> scsStrings {
127 {"ABCXYZ", "ABZ"},
128 {"ABZ", "ABCXYZ"},
129 {"AGGTAB", "GXTXAYB"},
130 {"X", "Y"},
131 };
132
133 // calculated output vector by scs function
134 std::vector <std::string> calculatedOutput(4, "");
135 int i=0;
136 for(auto & scsString : scsStrings) {
137
138 calculatedOutput[i] = dynamic_programming::shortest_common_supersequence::scs(
139 scsString[0], scsString[1]
140 );
141 i++;
142 }
143
144 // expected output vector acc to problem statement
145 std::vector <std::string> expectedOutput {
146 "ABCXYZ",
147 "ABCXYZ",
148 "AGGXTXAYB",
149 "XY"
150 };
151
152 // Testing implementation via assert function
153 // It will throw error if any of the expected test fails
154 // Else it will give nothing
155 for(int i=0; i < scsStrings.size(); i++) {
156 assert(expectedOutput[i] == calculatedOutput[i]);
157 }
158
159 std::cout << "All tests passed successfully!\n";
160 return;
161}
162
164int main() {
165 // test for implementation
166 test();
167
168 // user input
169 std::string s1, s2;
170 std::cin >> s1;
171 std::cin >> s2;
172
173 std::string ans;
174
175 // user output
176 ans = dynamic_programming::shortest_common_supersequence::scs(s1, s2);
177 std::cout << ans;
178 return 0;
179}
Dynamic Programming algorithms.
Shortest Common Super Sequence algorithm.
static void test()
std::string scs(const std::string &str1, const std::string &str2)