A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/da/d0d/longest__common__string_8cpp_source.html below:

TheAlgorithms/C++: dynamic_programming/longest_common_string.cpp Source File

Go to the documentation of this file. 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));

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];

41

sub_sols[pos_a][pos_b] = std::max(sub_sols[pos_a + 1][pos_b],

42

sub_sols[pos_a][pos_b + 1]);

47 return

sub_sols[0][0];

55 const

std::string string_a;

56 const

std::string string_b;

57 const

std::size_t common_string_len;

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

76 TestCase

(

"BADANA"

,

"CANADAS"

, 3),

77 TestCase

(

"a1a234a5aaaa6"

,

"A1AAAA234AAA56AAAAA"

, 6),

90template

<

typename

TestCases>

92 for

(

const auto

& cur_tc : test_cases) {

94

cur_tc.common_string_len);

104template

<

typename

TestCases>

107 for

(

const auto

& cur_tc : test_cases) {

109

cur_tc.common_string_len);

120 return

{in_str.rbegin(), in_str.rend()};

129template

<

typename

TestCases>

132 for

(

const auto

& cur_tc : test_cases) {

135

cur_tc.common_string_len);

144

assert(test_cases.size() > 0);

149

std::cout <<

"All tests have successfully passed!\n"

;

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4