|
1 |
| -#include <iosrteam> |
2 |
| -using namespace std; |
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief contains the definition of the function ::longest_common_string_length |
| 4 | + * @details |
| 5 | + * the function ::longest_common_string_length computes the length |
| 6 | + * of the longest common string which can be created of two input strings |
| 7 | + * by removing characters from them |
| 8 | + * |
| 9 | + * @author [Nikhil Arora](https://github.com/nikhilarora068) |
| 10 | + * @author [Piotr Idzik](https://github.com/vil02) |
| 11 | + */ |
3 | 12 |
|
4 |
| -int max(int a, int b) { return (a > b) ? a : b; } |
| 13 | +#include <cassert> /// for assert |
| 14 | +#include <iostream> /// for std::cout |
| 15 | +#include <string> /// for std::string |
| 16 | +#include <utility> /// for std::move |
| 17 | +#include <vector> /// for std::vector |
5 | 18 |
|
6 |
| -int main() { |
7 |
| - char str1[] = "DEFBCD"; |
8 |
| - char str2[] = "ABDEFJ"; |
9 |
| - int i, j, k; |
10 |
| - int n = strlen(str1) + 1; |
11 |
| - int m = strlen(str2) + 1; |
12 |
| - // cout<<n<<" "<<m<<"\n"; |
13 |
| - int a[m][n]; |
14 |
| - |
15 |
| - for (i = 0; i < m; i++) { |
16 |
| - for (j = 0; j < n; j++) { |
17 |
| - if (i == 0 || j == 0) |
18 |
| - a[i][j] = 0; |
19 |
| - |
20 |
| - else if (str1[i - 1] == str2[j - 1]) |
21 |
| - a[i][j] = a[i - 1][j - 1] + 1; |
22 |
| - |
23 |
| - else |
24 |
| - a[i][j] = 0; |
25 |
| - } |
26 |
| - } |
| 19 | +/** |
| 20 | + * @brief computes the length of the longest common string created from input |
| 21 | + * strings |
| 22 | + * @details has O(str_a.size()*str_b.size()) time and memory complexity |
| 23 | + * @param string_a first input string |
| 24 | + * @param string_b second input string |
| 25 | + * @returns the length of the longest common string which can be strated from |
| 26 | + * str_a and str_b |
| 27 | + */ |
| 28 | +std::size_t longest_common_string_length(const std::string& string_a, |
| 29 | + const std::string& string_b) { |
| 30 | + const auto size_a = string_a.size(); |
| 31 | + const auto size_b = string_b.size(); |
| 32 | + std::vector<std::vector<std::size_t>> sub_sols( |
| 33 | + size_a + 1, std::vector<std::size_t>(size_b + 1, 0)); |
27 | 34 |
|
28 |
| - /*for(i=0;i<m;i++) |
29 |
| - { |
30 |
| - for(j=0;j<n;j++) |
31 |
| - cout<<a[i][j]<<" "; |
32 |
| - cout<<"\n"; |
33 |
| - }*/ |
34 |
| - |
35 |
| - int ma = -1; |
36 |
| - int indi, indj; |
37 |
| - for (i = 0; i < m; i++) { |
38 |
| - for (j = 0; j < n; j++) { |
39 |
| - if (a[i][j] > ma) { |
40 |
| - ma = a[i][j]; |
41 |
| - indi = i; |
42 |
| - indj = j; |
| 35 | + const auto limit = static_cast<std::size_t>(-1); |
| 36 | + for (std::size_t pos_a = size_a - 1; pos_a != limit; --pos_a) { |
| 37 | + for (std::size_t pos_b = size_b - 1; pos_b != limit; --pos_b) { |
| 38 | + if (string_a[pos_a] == string_b[pos_b]) { |
| 39 | + sub_sols[pos_a][pos_b] = 1 + sub_sols[pos_a + 1][pos_b + 1]; |
| 40 | + } else { |
| 41 | + sub_sols[pos_a][pos_b] = std::max(sub_sols[pos_a + 1][pos_b], |
| 42 | + sub_sols[pos_a][pos_b + 1]); |
43 | 43 | }
|
44 | 44 | }
|
45 | 45 | }
|
46 | 46 |
|
47 |
| - cout << str1 << "\n"; |
48 |
| - cout << str2 << "\n"; |
| 47 | + return sub_sols[0][0]; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * @brief represents single example inputs and expected output of the function |
| 52 | + * ::longest_common_string_length |
| 53 | + */ |
| 54 | +struct TestCase { |
| 55 | + const std::string string_a; |
| 56 | + const std::string string_b; |
| 57 | + const std::size_t common_string_len; |
| 58 | + |
| 59 | + TestCase(std::string string_a, std::string string_b, |
| 60 | + const std::size_t in_common_string_len) |
| 61 | + : string_a(std::move(string_a)), |
| 62 | + string_b(std::move(string_b)), |
| 63 | + common_string_len(in_common_string_len) {} |
| 64 | +}; |
49 | 65 |
|
50 |
| - cout << "longest string size = " << ma /*<<" "<<indi<<" "<<indj*/ << "\n"; |
51 |
| - for (i = indi - 3; i < indi; i++) cout << str1[i]; |
52 |
| - cout << "\n"; |
| 66 | +/** |
| 67 | + * @return example data used in the tests of ::longest_common_string_length |
| 68 | + */ |
| 69 | +std::vector<TestCase> get_test_cases() { |
| 70 | + return {TestCase("", "", 0), |
| 71 | + TestCase("ab", "ab", 2), |
| 72 | + TestCase("ab", "ba", 1), |
| 73 | + TestCase("", "xyz", 0), |
| 74 | + TestCase("abcde", "ace", 3), |
| 75 | + TestCase("BADANA", "ANADA", 3), |
| 76 | + TestCase("BADANA", "CANADAS", 3), |
| 77 | + TestCase("a1a234a5aaaa6", "A1AAAA234AAA56AAAAA", 6), |
| 78 | + TestCase("123x", "123", 3), |
| 79 | + TestCase("12x3x", "123", 3), |
| 80 | + TestCase("1x2x3x", "123", 3), |
| 81 | + TestCase("x1x2x3x", "123", 3), |
| 82 | + TestCase("x12x3x", "123", 3)}; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * @brief checks the function ::longest_common_string_length agains example data |
| 87 | + * @param test_cases list of test cases |
| 88 | + * @tparam type representing a list of test cases |
| 89 | + */ |
| 90 | +template <typename TestCases> |
| 91 | +static void test_longest_common_string_length(const TestCases& test_cases) { |
| 92 | + for (const auto& cur_tc : test_cases) { |
| 93 | + assert(longest_common_string_length(cur_tc.string_a, cur_tc.string_b) == |
| 94 | + cur_tc.common_string_len); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * @brief checks if the function ::longest_common_string_length returns the same |
| 100 | + * result when its argument are flipped |
| 101 | + * @param test_cases list of test cases |
| 102 | + * @tparam type representing a list of test cases |
| 103 | + */ |
| 104 | +template <typename TestCases> |
| 105 | +static void test_longest_common_string_length_is_symmetric( |
| 106 | + const TestCases& test_cases) { |
| 107 | + for (const auto& cur_tc : test_cases) { |
| 108 | + assert(longest_common_string_length(cur_tc.string_b, cur_tc.string_a) == |
| 109 | + cur_tc.common_string_len); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * @brief reverses a given string |
| 115 | + * @param in_str input string |
| 116 | + * @return the string in which the characters appear in the reversed order as in |
| 117 | + * in_str |
| 118 | + */ |
| 119 | +std::string reverse_str(const std::string& in_str) { |
| 120 | + return {in_str.rbegin(), in_str.rend()}; |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * @brief checks if the function ::longest_common_string_length returns the same |
| 125 | + * result when its inputs are reversed |
| 126 | + * @param test_cases list of test cases |
| 127 | + * @tparam type representing a list of test cases |
| 128 | + */ |
| 129 | +template <typename TestCases> |
| 130 | +static void test_longest_common_string_length_for_reversed_inputs( |
| 131 | + const TestCases& test_cases) { |
| 132 | + for (const auto& cur_tc : test_cases) { |
| 133 | + assert(longest_common_string_length(reverse_str(cur_tc.string_a), |
| 134 | + reverse_str(cur_tc.string_b)) == |
| 135 | + cur_tc.common_string_len); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * @brief runs all tests for ::longest_common_string_length funcion |
| 141 | + */ |
| 142 | +static void tests() { |
| 143 | + const auto test_cases = get_test_cases(); |
| 144 | + assert(test_cases.size() > 0); |
| 145 | + test_longest_common_string_length(test_cases); |
| 146 | + test_longest_common_string_length_is_symmetric(test_cases); |
| 147 | + test_longest_common_string_length_for_reversed_inputs(test_cases); |
| 148 | + |
| 149 | + std::cout << "All tests have successfully passed!\n"; |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * @brief Main function |
| 154 | + * @returns 0 on exit |
| 155 | + */ |
| 156 | +int main() { |
| 157 | + tests(); |
| 158 | + return 0; |
53 | 159 | }
|
0 commit comments