Longest common subsequence: Difference between revisions

Resorted to verbose typedef and parameter names to help clarify their purpose.
m (Switched std::map to C++11 std::unordered_map to reduce initial Match() overhead.)
(Resorted to verbose typedef and parameter names to help clarify their purpose.)
Line 490:
typedef deque<uint32_t> THRESHOLD;
typedef deque<uint32_t> INDEXES;
typedef unordered_map<char, INDEXES> CHAR2INDEXESCHAR_TO_INDEXES_MAP;
typedef deque<INDEXES*> MATCHES;
 
uint32_t Pairs(MATCHES& matchesindexesOf2MatchedByIndex1, shared_ptr<Pair>* pairs) {
// The following implements the Hunt and Szymanski algorithm:
uint32_t Pairs(MATCHES& matches, shared_ptr<Pair>* pairs) {
auto trace = pairs != nullptr;
PAIRS traces;
Line 504 ⟶ 503:
//
uint32_t index1 = 0;
for (const auto& it1 : matchesindexesOf2MatchedByIndex1) {
if (!it1->empty()) {
auto dq2 = *it1;
Line 571 ⟶ 570:
 
//
// Match() avoids incurring m*n comparisons by using theCHAR_TO_INDEXES_MAP associativeto
// memory implemented by CHAR2INDEXES to achieve O(m+n) performance, where m and n are the input lengths.
// where m and n are the input lengths.
//
// The lookup time can be assumed constant in the case of characters.
Line 579 ⟶ 577:
// time will be O(log(m+n)), at most.
//
void Match(CHAR2INDEXESCHAR_TO_INDEXES_MAP& indexesindexesOf2MatchedByChar, MATCHES& matchesindexesOf2MatchedByIndex1,
const string& s1, const string& s2) {
uint32_t index = 0;
for (const auto& it : s2)
indexesindexesOf2MatchedByChar[it].push_back(index++);
 
for (const auto& it : s1) {
auto& dq2 = indexesindexesOf2MatchedByChar[it];
matchesindexesOf2MatchedByIndex1.push_back(&dq2);
}
}
Line 604 ⟶ 602:
public:
string Correspondence(const string& s1, const string& s2) {
CHAR_TO_INDEXES_MAP indexesOf2MatchedByChar;
CHAR2INDEXES indexes;
MATCHES matchesindexesOf2MatchedByIndex1; // holds references into indexesindexesOf2MatchedByChar
Match(indexesindexesOf2MatchedByChar, matchesindexesOf2MatchedByIndex1, s1, s2);
shared_ptr<Pair> pairs; // obtain the LCS as index pairs
auto length = Pairs(matchesindexesOf2MatchedByIndex1, &pairs);
return Select(pairs, length, false, s1, s2);
}
159

edits