42 std::string
scs(
const std::string &str1,
const std::string &str2) {
46 if(str1.empty() && str2.empty()) {
49 else if(str1.empty()) {
52 else if(str2.empty()) {
57 std::vector <std::vector <int>> lookup(str1.length() + 1, std::vector <int> (str2.length() + 1, 0));
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;
65 lookup[i][j] = std::max(lookup[i-1][j], lookup[i][j-1]);
81 if(str1[i-1] == str2[j-1]) {
82 s.push_back(str1[i-1]);
88 if(lookup[i-1][j] > lookup[i][j-1]) {
89 s.push_back(str1[i-1]);
93 s.push_back(str2[j-1]);
102 s.push_back(str1[i-1]);
108 s.push_back(str2[j-1]);
114 reverse(s.begin(), s.end());
126 std::vector <std::vector <std::string>> scsStrings {
129 {
"AGGTAB",
"GXTXAYB"},
134 std::vector <std::string> calculatedOutput(4,
"");
136 for(
auto & scsString : scsStrings) {
138 calculatedOutput[i] = dynamic_programming::shortest_common_supersequence::scs(
139 scsString[0], scsString[1]
145 std::vector <std::string> expectedOutput {
155 for(
int i=0; i < scsStrings.size(); i++) {
156 assert(expectedOutput[i] == calculatedOutput[i]);
159 std::cout <<
"All tests passed successfully!\n";