Algorithms_in_C++ 1.0.0
Set of 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:

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)
 for std::vector
 
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

Function Documentation

◆ get_test_cases()

std::vector< TestCase > get_test_cases ( )
Returns
example data used in the tests of longest_common_string_length
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
Definition longest_common_string.cpp:54

◆ longest_common_string_length()

std::size_t longest_common_string_length ( const std::string & string_a,
const std::string & string_b )

for std::vector

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

computes the length of the longest common string created from input strings

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
29 {
30 const auto size_a = string_a.size();
31 const auto size_b = string_b.size();
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}
T max(T... args)
T size(T... args)
Here is the call graph for this function:

◆ main()

int main ( void )

Main function.

Returns
0 on exit
156 {
157 tests();
158 return 0;
159}
static void tests()
runs all tests for longest_common_string_length funcion
Definition longest_common_string.cpp:142
Here is the call graph for this function:

◆ 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
119 {
120 return {in_str.rbegin(), in_str.rend()};
121}
T rbegin(T... args)
T rend(T... args)
Here is the call graph for this function:

◆ 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
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)
for std::vector
Definition longest_common_string.cpp:28
Here is the call graph for this function:

◆ 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
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
Definition longest_common_string.cpp:119
Here is the call graph for this function:

◆ 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
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}
Here is the call graph for this function:

◆ tests()

static void tests ( )
static

runs all tests for longest_common_string_length funcion

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...
Definition longest_common_string.cpp:130
std::vector< TestCase > get_test_cases()
Definition longest_common_string.cpp:69
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
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
Here is the call graph for this function: