style: use string instead of str

This commit is contained in:
Piotr Idzik
2023-05-30 11:14:59 +00:00
parent 6e7fd805a0
commit da2567469a

View File

@@ -16,22 +16,22 @@
* @brief computes the length of the longest common string created from input
* strings
* @details has O(str_a.size()*str_b.size()) time and memory complexity
* @param str_a first input string
* @param str_b second input string
* @param string_a first input string
* @param string_b second input string
* @returns the length of the longest common string which can be strated from
* str_a and str_b
*/
std::size_t longest_common_string_length(const std::string& str_a,
const std::string& str_b) {
const auto size_a = str_a.size();
const auto size_b = str_b.size();
std::size_t longest_common_string_length(const std::string& string_a,
const std::string& string_b) {
const auto size_a = string_a.size();
const auto size_b = string_b.size();
std::vector<std::vector<std::size_t>> sub_sols(
size_a + 1, std::vector<std::size_t>(size_b + 1, 0));
const auto limit = static_cast<std::size_t>(-1);
for (std::size_t pos_a = size_a - 1; pos_a != limit; --pos_a) {
for (std::size_t pos_b = size_b - 1; pos_b != limit; --pos_b) {
if (str_a[pos_a] == str_b[pos_b]) {
if (string_a[pos_a] == string_b[pos_b]) {
sub_sols[pos_a][pos_b] = 1 + sub_sols[pos_a + 1][pos_b + 1];
} else {
sub_sols[pos_a][pos_b] = std::max(sub_sols[pos_a + 1][pos_b],
@@ -44,14 +44,14 @@ std::size_t longest_common_string_length(const std::string& str_a,
}
struct TestCase {
const std::string str_a;
const std::string str_b;
const std::string string_a;
const std::string string_b;
const std::size_t common_string_len;
TestCase(std::string in_str_a, std::string in_str_b,
TestCase(std::string string_a, std::string string_b,
const std::size_t in_common_string_len)
: str_a(std::move(in_str_a)),
str_b(std::move(in_str_b)),
: string_a(std::move(string_a)),
string_b(std::move(string_b)),
common_string_len(in_common_string_len) {}
};
@@ -79,7 +79,7 @@ std::vector<TestCase> get_test_cases() {
template <typename TestCases>
void test_longest_common_string_length(const TestCases& test_cases) {
for (const auto& cur_tc : test_cases) {
assert(longest_common_string_length(cur_tc.str_a, cur_tc.str_b) ==
assert(longest_common_string_length(cur_tc.string_a, cur_tc.string_b) ==
cur_tc.common_string_len);
}
}
@@ -94,7 +94,7 @@ template <typename TestCases>
void test_longest_common_string_length_is_symmetric(
const TestCases& test_cases) {
for (const auto& cur_tc : test_cases) {
assert(longest_common_string_length(cur_tc.str_b, cur_tc.str_a) ==
assert(longest_common_string_length(cur_tc.string_b, cur_tc.string_a) ==
cur_tc.common_string_len);
}
}
@@ -119,8 +119,8 @@ template <typename TestCases>
void test_longest_common_string_length_for_reversed_inputs(
const TestCases& test_cases) {
for (const auto& cur_tc : test_cases) {
assert(longest_common_string_length(reverse_str(cur_tc.str_a),
reverse_str(cur_tc.str_b)) ==
assert(longest_common_string_length(reverse_str(cur_tc.string_a),
reverse_str(cur_tc.string_b)) ==
cur_tc.common_string_len);
}
}