16
16
* @brief computes the length of the longest common string created from input
17
17
* strings
18
18
* @details has O(str_a.size()*str_b.size()) time and memory complexity
19
- * @param str_a first input string
20
- * @param str_b second input string
19
+ * @param string_a first input string
20
+ * @param string_b second input string
21
21
* @returns the length of the longest common string which can be strated from
22
22
* str_a and str_b
23
23
*/
24
- std::size_t longest_common_string_length (const std::string& str_a ,
25
- const std::string& str_b ) {
26
- const auto size_a = str_a .size ();
27
- const auto size_b = str_b .size ();
24
+ std::size_t longest_common_string_length (const std::string& string_a ,
25
+ const std::string& string_b ) {
26
+ const auto size_a = string_a .size ();
27
+ const auto size_b = string_b .size ();
28
28
std::vector<std::vector<std::size_t >> sub_sols (
29
29
size_a + 1 , std::vector<std::size_t >(size_b + 1 , 0 ));
30
30
31
31
const auto limit = static_cast <std::size_t >(-1 );
32
32
for (std::size_t pos_a = size_a - 1 ; pos_a != limit; --pos_a) {
33
33
for (std::size_t pos_b = size_b - 1 ; pos_b != limit; --pos_b) {
34
- if (str_a [pos_a] == str_b [pos_b]) {
34
+ if (string_a [pos_a] == string_b [pos_b]) {
35
35
sub_sols[pos_a][pos_b] = 1 + sub_sols[pos_a + 1 ][pos_b + 1 ];
36
36
} else {
37
37
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,
44
44
}
45
45
46
46
struct TestCase {
47
- const std::string str_a ;
48
- const std::string str_b ;
47
+ const std::string string_a ;
48
+ const std::string string_b ;
49
49
const std::size_t common_string_len;
50
50
51
- TestCase (std::string in_str_a , std::string in_str_b ,
51
+ TestCase (std::string string_a , std::string string_b ,
52
52
const std::size_t in_common_string_len)
53
- : str_a (std::move(in_str_a )),
54
- str_b (std::move(in_str_b )),
53
+ : string_a (std::move(string_a )),
54
+ string_b (std::move(string_b )),
55
55
common_string_len(in_common_string_len) {}
56
56
};
57
57
@@ -79,7 +79,7 @@ std::vector<TestCase> get_test_cases() {
79
79
template <typename TestCases>
80
80
void test_longest_common_string_length (const TestCases& test_cases) {
81
81
for (const auto & cur_tc : test_cases) {
82
- assert (longest_common_string_length (cur_tc.str_a , cur_tc.str_b ) ==
82
+ assert (longest_common_string_length (cur_tc.string_a , cur_tc.string_b ) ==
83
83
cur_tc.common_string_len );
84
84
}
85
85
}
@@ -94,7 +94,7 @@ template <typename TestCases>
94
94
void test_longest_common_string_length_is_symmetric (
95
95
const TestCases& test_cases) {
96
96
for (const auto & cur_tc : test_cases) {
97
- assert (longest_common_string_length (cur_tc.str_b , cur_tc.str_a ) ==
97
+ assert (longest_common_string_length (cur_tc.string_b , cur_tc.string_a ) ==
98
98
cur_tc.common_string_len );
99
99
}
100
100
}
@@ -119,8 +119,8 @@ template <typename TestCases>
119
119
void test_longest_common_string_length_for_reversed_inputs (
120
120
const TestCases& test_cases) {
121
121
for (const auto & cur_tc : test_cases) {
122
- assert (longest_common_string_length (reverse_str (cur_tc.str_a ),
123
- reverse_str (cur_tc.str_b )) ==
122
+ assert (longest_common_string_length (reverse_str (cur_tc.string_a ),
123
+ reverse_str (cur_tc.string_b )) ==
124
124
cur_tc.common_string_len );
125
125
}
126
126
}
0 commit comments