Skip to content

Commit

Permalink
Formatting 2
Browse files Browse the repository at this point in the history
  • Loading branch information
SemaiCZE committed May 31, 2018
1 parent 6936ffa commit 675f202
Show file tree
Hide file tree
Showing 10 changed files with 1,586 additions and 1,517 deletions.
87 changes: 40 additions & 47 deletions judges/recodex_token_judge/bpplib/algo/lcs.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Author: Martin Krulis <krulis@ksi.mff.cuni.cz>
* Last Modification: 24.5.2018
* License: CC 3.0 BY-NC (http://creativecommons.org/)
*/
* Author: Martin Krulis <krulis@ksi.mff.cuni.cz>
* Last Modification: 24.5.2018
* License: CC 3.0 BY-NC (http://creativecommons.org/)
*/
#ifndef BPPLIB_ALGO_LCS_HPP
#define BPPLIB_ALGO_LCS_HPP

Expand All @@ -21,27 +21,24 @@ namespace bpp
* \tparma COMPARATOR Comparator class holds a static method compare(seq1, i1, seq2, i2) -> bool.
* I.e., the comparator is also responsible for fetching values from the seq. containers.
*/
template<typename RES = std::size_t, class CONTAINER, typename COMPARATOR>
template <typename RES = std::size_t, class CONTAINER, typename COMPARATOR>
RES longest_common_subsequence_length(const CONTAINER &sequence1, const CONTAINER &sequence2, COMPARATOR comparator)
{
if (sequence1.size() == 0 || sequence2.size() == 0) return (RES)0;
if (sequence1.size() == 0 || sequence2.size() == 0) return (RES) 0;

// Make sure in seq1 is the longer sequence ...
const CONTAINER &seq1 = sequence1.size() >= sequence2.size() ? sequence1 : sequence2;
const CONTAINER &seq2 = sequence1.size() < sequence2.size() ? sequence1 : sequence2;

std::vector<RES> row((std::size_t)seq2.size());
std::size_t rows = (std::size_t)seq1.size();
std::vector<RES> row((std::size_t) seq2.size());
std::size_t rows = (std::size_t) seq1.size();

// Dynamic programming - matrix traversal that keeps only the last row.
for (std::size_t r = 0; r < rows; ++r) {
RES lastUpperLeft = 0, lastLeft = 0;
for (std::size_t i = 0; i < row.size(); ++i) {
RES upper = row[i];
row[i] =
(comparator(seq1, r, seq2, i))
? lastUpperLeft + 1
: std::max(lastLeft, upper);
row[i] = (comparator(seq1, r, seq2, i)) ? lastUpperLeft + 1 : std::max(lastLeft, upper);
lastLeft = row[i];
lastUpperLeft = upper;
}
Expand All @@ -52,29 +49,30 @@ namespace bpp


// Only an overload that uses default comparator.
template<typename RES = std::size_t, class CONTAINER>
template <typename RES = std::size_t, class CONTAINER>
RES longest_common_subsequence_length(const CONTAINER &sequence1, const CONTAINER &sequence2)
{
return longest_common_subsequence_length<RES>(sequence1, sequence2,
return longest_common_subsequence_length<RES>(sequence1,
sequence2,
[](const CONTAINER &seq1, std::size_t i1, const CONTAINER &seq2, std::size_t i2) -> bool {
return seq1[i1] == seq2[i2];
}
);
});
}



/**
* Implements a longes common subsequence algorithm, which founds only the length of the LCS itself.
* \tparam RES The result type (must be an integral type).
* \tparam CONTAINER Class holding the sequence. The class must have size() method
* and the comparator must be able to get values from the container based on their indices.
* \tparma COMPARATOR Comparator class holds a static method compare(seq1, i1, seq2, i2) -> bool.
* I.e., the comparator is also responsible for fetching values from the seq. containers.
*/
template<typename IDX = std::size_t, class CONTAINER, typename COMPARATOR>
void longest_common_subsequence(const CONTAINER &sequence1, const CONTAINER &sequence2,
std::vector<std::pair<IDX, IDX>> &common, COMPARATOR comparator)
* Implements a longes common subsequence algorithm, which founds only the length of the LCS itself.
* \tparam RES The result type (must be an integral type).
* \tparam CONTAINER Class holding the sequence. The class must have size() method
* and the comparator must be able to get values from the container based on their indices.
* \tparma COMPARATOR Comparator class holds a static method compare(seq1, i1, seq2, i2) -> bool.
* I.e., the comparator is also responsible for fetching values from the seq. containers.
*/
template <typename IDX = std::size_t, class CONTAINER, typename COMPARATOR>
void longest_common_subsequence(const CONTAINER &sequence1,
const CONTAINER &sequence2,
std::vector<std::pair<IDX, IDX>> &common,
COMPARATOR comparator)
{
struct Node {
std::pair<IDX, IDX> previous;
Expand All @@ -90,33 +88,27 @@ namespace bpp

// Prepare vector representing the LCS matrix ...
std::vector<Node> matrix((size1 + 1) * (size2 + 1));
for (std::size_t i = 0; i < size1; ++i) {
matrix[i + 1].previous.first = 1;
}
for (std::size_t i = 0; i < size2; ++i) {
matrix[(i + 1)*(size1 + 1)].previous.second = 1;
}
for (std::size_t i = 0; i < size1; ++i) { matrix[i + 1].previous.first = 1; }
for (std::size_t i = 0; i < size2; ++i) { matrix[(i + 1) * (size1 + 1)].previous.second = 1; }

// Fill in the LCS matrix by dynamic programming
std::size_t i = size1 + 2; // current position in matrix (i == (c+1)*(size1+1) + (r+1))
for (std::size_t r = 0; r < size2; ++r) { // iterate over rows
for (std::size_t c = 0; c < size1; ++c) { // iterate over cols
matrix[i].match = comparator(sequence1, c, sequence2, r);

if (matrix[i].match) {
// Matching tokens should prolong the sequence...
matrix[i].length = matrix[i - size1 - 2].length + 1;
matrix[i].previous.first = 1;
matrix[i].previous.second = 1;
}
else {
} else {
IDX leftLength = matrix[i - 1].length;
IDX upperLength = matrix[i - size1 - 1].length;
if (leftLength >= upperLength) {
matrix[i].previous.first = 1;
matrix[i].length = leftLength;
}
else {
} else {
matrix[i].previous.second = 1;
matrix[i].length = upperLength;
}
Expand All @@ -131,9 +123,7 @@ namespace bpp
std::size_t r = size2;
while (c > 0 && r > 0) {
const Node &node = matrix[r * (size1 + 1) + c];
if (node.match) {
common.push_back(std::make_pair<IDX, IDX>(c - 1, r - 1));
}
if (node.match) { common.push_back(std::make_pair<IDX, IDX>(c - 1, r - 1)); }

c -= node.previous.first;
r -= node.previous.second;
Expand All @@ -145,16 +135,19 @@ namespace bpp


// Only an overload that uses default comparator.
template<typename IDX = std::size_t, class CONTAINER>
void longest_common_subsequence(const CONTAINER &sequence1, const CONTAINER &sequence2, std::vector<std::pair<IDX, IDX>> &common)
template <typename IDX = std::size_t, class CONTAINER>
void longest_common_subsequence(
const CONTAINER &sequence1, const CONTAINER &sequence2, std::vector<std::pair<IDX, IDX>> &common)
{
longest_common_subsequence<IDX>(sequence1, sequence2, common,
longest_common_subsequence<IDX>(sequence1,
sequence2,
common,
[](const CONTAINER &seq1, std::size_t i1, const CONTAINER &seq2, std::size_t i2) -> bool {
return seq1[i1] == seq2[i2];
});
return seq1[i1] == seq2[i2];
});
}


}
} // namespace bpp

#endif
Loading

0 comments on commit 675f202

Please sign in to comment.