TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
longest_common_string.cpp
Go to the documentation of this file.
1
13#include <cassert>
14#include <iostream>
15#include <string>
16#include <utility>
17#include <vector>
18
28std::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));
34
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 }
44 }
45 }
46
47 return sub_sols[0][0];
48}
49
54struct 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};
65
69std::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
90template <typename TestCases>
91static 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
104template <typename TestCases>
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
119std::string reverse_str(const std::string& in_str) {
120 return {in_str.rbegin(), in_str.rend()};
121}
122
129template <typename TestCases>
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
142static void tests() {
143 const auto test_cases = get_test_cases();
144 assert(test_cases.size() > 0);
148
149 std::cout << "All tests have successfully passed!\n";
150}
151
156int main() {
157 tests();
158 return 0;
159}
class encapsulating the necessary test cases
static void test_longest_common_string_length_for_reversed_inputs(const TestCases &test_cases)
checks if the function longest_common_string_length returns the same result when its inputs are rever...
static void tests()
runs all tests for longest_common_string_length funcion
std::vector< TestCase > get_test_cases()
static void test_longest_common_string_length(const TestCases &test_cases)
checks the function longest_common_string_length agains example data
std::size_t longest_common_string_length(const std::string &string_a, const std::string &string_b)
computes the length of the longest common string created from input strings
std::string reverse_str(const std::string &in_str)
reverses a given string
static void test_longest_common_string_length_is_symmetric(const TestCases &test_cases)
checks if the function longest_common_string_length returns the same result when its argument are fli...
int main()
Main function.
represents single example inputs and expected output of the function longest_common_string_length