std::string
scs(
conststd::string &str1,
conststd::string &str2) {
46 if(str1.empty() && str2.empty()) {
49 else if(str1.empty()) {
52 else if(str2.empty()) {
57std::vector <std::vector <int>> lookup(str1.length() + 1, std::vector <int> (str2.length() + 1, 0));
59 for(
inti=1; i <= str1.length(); i++) {
60 for(
intj=1; j <= str2.length(); j++) {
61 if(str1[i-1] == str2[j-1]) {
62lookup[i][j] = lookup[i-1][j-1] + 1;
65lookup[i][j] = std::max(lookup[i-1][j], lookup[i][j-1]);
81 if(str1[i-1] == str2[j-1]) {
82s.push_back(str1[i-1]);
88 if(lookup[i-1][j] > lookup[i][j-1]) {
89s.push_back(str1[i-1]);
93s.push_back(str2[j-1]);
102s.push_back(str1[i-1]);
108s.push_back(str2[j-1]);
114reverse(s.begin(), s.end());
126std::vector <std::vector <std::string>> scsStrings {
127{
"ABCXYZ",
"ABZ"},
128{
"ABZ",
"ABCXYZ"},
129{
"AGGTAB",
"GXTXAYB"},
134std::vector <std::string> calculatedOutput(4,
"");
136 for(
auto& scsString : scsStrings) {
139scsString[0], scsString[1]
145std::vector <std::string> expectedOutput {
155 for(
inti=0; i < scsStrings.size(); i++) {
156assert(expectedOutput[i] == calculatedOutput[i]);
159std::cout <<
"All tests passed successfully!\n";
Dynamic Programming algorithms.
Shortest Common Super Sequence algorithm.
std::string scs(const std::string &str1, const std::string &str2)
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