A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d7/d65/shortest__common__supersequence_8cpp.html below:

TheAlgorithms/C++: dynamic_programming/shortest_common_supersequence.cpp File Reference

Loading...

Searching...

No Matches

SCS is a string Z which is the shortest supersequence of strings X and Y (may not be continuous in Z, but order is maintained). More...

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cassert>

Go to the source code of this file.

SCS is a string Z which is the shortest supersequence of strings X and Y (may not be continuous in Z, but order is maintained).

The idea is to use lookup table method as used in LCS. For example: example 1:- X: 'ABCXYZ', Y: 'ABZ' then Z will be 'ABCXYZ' (y is not continuous but in order)

For example: example 2:- X: 'AGGTAB', Y: 'GXTXAYB' then Z will be 'AGGXTXAYB'

See also
more on SCS
related problem Leetcode

Definition in file shortest_common_supersequence.cpp.

◆ main()

Main function (driver code)

Definition at line 164 of file shortest_common_supersequence.cpp.

164 {

165

167

168

169 std::string s1, s2;

170 std::cin >> s1;

171 std::cin >> s2;

172

173 std::string ans;

174

175

177 std::cout << ans;

178 return 0;

179}

std::string scs(const std::string &str1, const std::string &str2)

◆ scs() std::string dynamic_programming::shortest_common_supersequence::scs ( const std::string & str1, const std::string & str2 )

Function implementing Shortest Common Super-Sequence algorithm using look-up table method.

Parameters
str1 first string 'X' str2 second string 'Y'
Returns
string 'Z', superSequence of X and Y

Definition at line 42 of file shortest_common_supersequence.cpp.

42 {

43

44

45

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

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

71

72

73 int i=str1.length();

74 int j=str2.length();

75 std::string s;

76

77 while(i>0 && j>0) {

78

79

80

81 if(str1[i-1] == str2[j-1]) {

82 s.push_back(str1[i-1]);

83 i--;

84 j--;

85 }

86

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

100

101 while(i > 0) {

102 s.push_back(str1[i-1]);

103 i--;

104 }

105

106

107 while(j > 0) {

108 s.push_back(str2[j-1]);

109 j--;

110 }

111

112

113

114 reverse(s.begin(), s.end());

115 return s;

116 }

◆ test()

Test Function

Returns
void

Definition at line 124 of file shortest_common_supersequence.cpp.

124 {

125

126 std::vector <std::vector <std::string>> scsStrings {

127 {"ABCXYZ", "ABZ"},

128 {"ABZ", "ABCXYZ"},

129 {"AGGTAB", "GXTXAYB"},

130 {"X", "Y"},

131 };

132

133

134 std::vector <std::string> calculatedOutput(4, "");

135 int i=0;

136 for(auto & scsString : scsStrings) {

137

139 scsString[0], scsString[1]

140 );

141 i++;

142 }

143

144

145 std::vector <std::string> expectedOutput {

146 "ABCXYZ",

147 "ABCXYZ",

148 "AGGXTXAYB",

149 "XY"

150 };

151

152

153

154

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}


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4