TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
longest_common_string.cpp
Go to the documentation of this file.
1
12
13
#include <cassert>
14
#include <iostream>
15
#include <string>
16
#include <utility>
17
#include <vector>
18
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));
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
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
};
65
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
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
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
119
std::string
reverse_str
(
const
std::string& in_str) {
120
return
{in_str.rbegin(), in_str.rend()};
121
}
122
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
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
156
int
main
() {
157
tests
();
158
return
0;
159
}
TestCases
class encapsulating the necessary test cases
Definition
inorder_successor_of_bst.cpp:225
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
test_longest_common_string_length_for_reversed_inputs
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...
Definition
longest_common_string.cpp:130
get_test_cases
std::vector< TestCase > get_test_cases()
Definition
longest_common_string.cpp:69
test_longest_common_string_length
static void test_longest_common_string_length(const TestCases &test_cases)
checks the function longest_common_string_length agains example data
Definition
longest_common_string.cpp:91
longest_common_string_length
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
Definition
longest_common_string.cpp:28
reverse_str
std::string reverse_str(const std::string &in_str)
reverses a given string
Definition
longest_common_string.cpp:119
test_longest_common_string_length_is_symmetric
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...
Definition
longest_common_string.cpp:105
tests
Testcases to check Union of Two Arrays.
TestCase
represents single example inputs and expected output of the function longest_common_string_length
Definition
longest_common_string.cpp:54
dynamic_programming
longest_common_string.cpp
Generated by
1.18.0