TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
longest_common_string.cpp File Reference

contains the definition of the function longest_common_string_length More...

#include <cassert>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
Include dependency graph for longest_common_string.cpp:

Go to the source code of this file.

Classes

struct  TestCase
 represents single example inputs and expected output of the function longest_common_string_length More...
 

Functions

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::vector< TestCaseget_test_cases ()
 
template<typename TestCases >
static void test_longest_common_string_length (const TestCases &test_cases)
 checks the function longest_common_string_length agains example data
 
template<typename TestCases >
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 flipped
 
std::string reverse_str (const std::string &in_str)
 reverses a given string
 
template<typename TestCases >
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 reversed
 
static void tests ()
 runs all tests for longest_common_string_length funcion
 
int main ()
 Main function.
 

Detailed Description

contains the definition of the function longest_common_string_length

the function longest_common_string_length computes the length of the longest common string which can be created of two input strings by removing characters from them

Author
Nikhil Arora
Piotr Idzik

Definition in file longest_common_string.cpp.

Function Documentation

◆ get_test_cases()

std::vector< TestCase > get_test_cases ( )
Returns
example data used in the tests of longest_common_string_length

Definition at line 69 of file longest_common_string.cpp.

69 {
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}
represents single example inputs and expected output of the function longest_common_string_length

◆ 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

for assert for std::cout for std::string for std::move for std::vector

has O(str_a.size()*str_b.size()) time and memory complexity

Parameters
string_afirst input string
string_bsecond input string
Returns
the length of the longest common string which can be strated from str_a and str_b

Definition at line 28 of file longest_common_string.cpp.

29 {
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}

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 156 of file longest_common_string.cpp.

156 {
157 tests();
158 return 0;
159}
static void tests()
runs all tests for longest_common_string_length funcion

◆ reverse_str()

std::string reverse_str ( const std::string & in_str)

reverses a given string

Parameters
in_strinput string
Returns
the string in which the characters appear in the reversed order as in in_str

Definition at line 119 of file longest_common_string.cpp.

119 {
120 return {in_str.rbegin(), in_str.rend()};
121}

◆ test_longest_common_string_length()

template<typename TestCases >
static void test_longest_common_string_length ( const TestCases & test_cases)
static

checks the function longest_common_string_length agains example data

Parameters
test_caseslist of test cases
Template Parameters
typerepresenting a list of test cases

Definition at line 91 of file longest_common_string.cpp.

91 {
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}
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

◆ test_longest_common_string_length_for_reversed_inputs()

template<typename TestCases >
static void test_longest_common_string_length_for_reversed_inputs ( const TestCases & test_cases)
static

checks if the function longest_common_string_length returns the same result when its inputs are reversed

Parameters
test_caseslist of test cases
Template Parameters
typerepresenting a list of test cases

Definition at line 130 of file longest_common_string.cpp.

131 {
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}
std::string reverse_str(const std::string &in_str)
reverses a given string

◆ test_longest_common_string_length_is_symmetric()

template<typename TestCases >
static void test_longest_common_string_length_is_symmetric ( const TestCases & test_cases)
static

checks if the function longest_common_string_length returns the same result when its argument are flipped

Parameters
test_caseslist of test cases
Template Parameters
typerepresenting a list of test cases

Definition at line 105 of file longest_common_string.cpp.

106 {
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}

◆ tests()

static void tests ( )
static

runs all tests for longest_common_string_length funcion

Definition at line 142 of file longest_common_string.cpp.

142 {
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}
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...
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
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...