Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 32db99c

Browse files
vil02github-actions[bot]realstealthninjaPanquesito7
authored
[fix/docs]: cleanup longest_common_string.cpp (TheAlgorithms#2462)
* updating DIRECTORY.md * fix: cleanup longest_common_string.cpp * clang-format and clang-tidy fixes for 3280d46 * docs: explain why utility header is needed * refactor: add test_all() * docs: add relevant doc-strs * refactor: rename to tests() * style: typo fix * style: use string instead of str * Apply suggestions from code review The code after this commit requires reformatting. Co-authored-by: David Leal <halfpacho@gmail.com> * clang-format and clang-tidy fixes for 18203b2 * style: give an exact reason why iostream is needed * style: make all test functions `static` * docs: update missing docs * chore: apply suggestions from code review --------- Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com> Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Co-authored-by: David Leal <halfpacho@gmail.com>
1 parent e3f0551 commit 32db99c

File tree

1 file changed

+150
-44
lines changed

1 file changed

+150
-44
lines changed
Lines changed: 150 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,159 @@
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+
*/
312

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
518

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));
2734

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]);
4343
}
4444
}
4545
}
4646

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+
};
4965

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;
53159
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /