mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-13 17:50:45 +08:00
feat: update to CXX standard 17 and add CMakeLists file to directories without them (#2746)
* chore: add cache and build comment to git ignore
* fix: add cmakelists to dynamic programming
* fix: add cmakelists to greedy_algorithms
* fix: add cmakelists to operations_on_datastructures
* fix: add cmakelists to range_queries
* fix: add `dynamic_programmin`, `greedy_algorithms`, `range_queries` and `operations_on_datastructures` subdirectories to cmakelists.txt
* fix: init of transform_reduce in dynamic_programming
* fix: add an include for functional in catalan_numbers
* chore: bump CXX standard to 20
* revert: bump CXX standard to 20
* chore: bump c++ version to 17 and add justification
Arm supports c++ 17
Esp32 supports c++ 23
decision was made to be 17 because it seemed to offer the best combatability
* fix: compilation error in catalan numbers
* fix: add <set> header to longest increasing subsequence nlogn
* fix: add cmath & algorithm header to mo.cpp
* fix: remove register key word from fast integer
* fix: replace using namespace std with std::cin and std::cout
* docs: typo in c++17
* fix: memory leak in bellman_ford
* fix: typo in bellman_ford
* fix: typo in word_break
* fix: dynamic array in coin_change
* fix dynamic array in egg_dropping puzzle
* chore: remove unnecessary comment
* fix: add vla to be an error
* chore: add extra warnings
* fix: use add_compile options instead of set()
* fix: compile options are not strings
* fix: vla in floyd_warshall
* fix: vla in egg_dropping_puzzel
* fix: vla in coin_change
* fix: vla in edit_distance
* fix: vla in floyd_warshall
* feat: remove kadane and replace it with kadane2
* fix: vla in longest_common_subsequence
* fix: int overflow in floyd_warshall
* fix: vla in lisnlogn
* fix: use const vector& instead of array
* fix: use dynamic array instead of vla in knapsack
* fix: use of and in msvc is unsupported by default adding permissive flag fixes it
* test: make executables the tests themselves
* Revert "test: make executables the tests themselves"
This reverts commit 7a16c31c4e.
* fix: make dist constant in print
* fix: namespace issue in unbounded_0_1
* fix: include cstdint to fix compilation
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -34,5 +34,9 @@ a.out
|
||||
*.out
|
||||
*.app
|
||||
|
||||
# Cache
|
||||
.cache/
|
||||
|
||||
# Build
|
||||
build/
|
||||
git_diff.txt
|
||||
|
||||
@@ -5,16 +5,17 @@ project(Algorithms_in_C++
|
||||
DESCRIPTION "Set of algorithms implemented in C++."
|
||||
)
|
||||
|
||||
# set(CMAKE_CXX_CPPLINT "~/anaconda3/bin/cpplint --filter=-legal/copyright --std=c++11")
|
||||
# find_program(CLANG_FORMAT "clang-format")
|
||||
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
# C++ standard
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
# Additional warnings and errors
|
||||
if(MSVC)
|
||||
# set(CMAKE_CXX_STANDARD 14)
|
||||
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
|
||||
endif(MSVC)
|
||||
add_compile_options(/W4 /permissive-)
|
||||
else()
|
||||
add_compile_options(-Wall -Wextra -Wno-register -Werror=vla)
|
||||
endif()
|
||||
|
||||
option(USE_OPENMP "flag to use OpenMP for multithreading" ON)
|
||||
if(USE_OPENMP)
|
||||
@@ -38,6 +39,10 @@ add_subdirectory(graphics)
|
||||
add_subdirectory(probability)
|
||||
add_subdirectory(backtracking)
|
||||
add_subdirectory(bit_manipulation)
|
||||
add_subdirectory(dynamic_programming)
|
||||
add_subdirectory(greedy_algorithms)
|
||||
add_subdirectory(range_queries)
|
||||
add_subdirectory(operations_on_datastructures)
|
||||
add_subdirectory(data_structures)
|
||||
add_subdirectory(machine_learning)
|
||||
add_subdirectory(numerical_methods)
|
||||
|
||||
@@ -22,7 +22,7 @@ This repository is a collection of open-source implementation of a variety of al
|
||||
* Well documented source code with detailed explanations provide a valuable resource for educators and students alike.
|
||||
* Each source code is atomic using [STL classes](https://en.wikipedia.org/wiki/Standard_Template_Library) and _no external libraries_ are required for their compilation and execution. Thus, the fundamentals of the algorithms can be studied in much depth.
|
||||
* Source codes are [compiled and tested](https://github.com/TheAlgorithms/C-Plus-Plus/actions?query=workflow%3A%22Awesome+CI+Workflow%22) for every commit on the latest versions of three major operating systems viz., Windows, MacOS, and Ubuntu (Linux) using MSVC 19 2022, AppleClang 14.0.0, and GNU 11.3.0 respectively.
|
||||
* Strict adherence to [C++11](https://en.wikipedia.org/wiki/C%2B%2B11) standard ensures portability of code to embedded systems as well like ESP32, ARM Cortex, etc. with little to no changes.
|
||||
* Strict adherence to [C++17](https://en.wikipedia.org/wiki/C%2B%2B17) standard ensures portability of code to embedded systems as well like [ESP32](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/cplusplus.html#c-language-standard), [ARM Cortex](https://developer.arm.com/documentation/101458/2404/Standards-support/Supported-C-C---standards-in-Arm-C-C---Compiler), etc. with little to no changes.
|
||||
* Self-checks within programs ensure correct implementations with confidence.
|
||||
* Modular implementations and OpenSource licensing enable the functions to be utilized conveniently in other applications.
|
||||
|
||||
|
||||
18
dynamic_programming/CMakeLists.txt
Normal file
18
dynamic_programming/CMakeLists.txt
Normal file
@@ -0,0 +1,18 @@
|
||||
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
||||
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
||||
# automatically.
|
||||
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
|
||||
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
|
||||
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
|
||||
foreach( testsourcefile ${APP_SOURCES} )
|
||||
# I used a simple string replace, to cut off .cpp.
|
||||
string( REPLACE ".cpp" "" testname ${testsourcefile} )
|
||||
add_executable( ${testname} ${testsourcefile} )
|
||||
|
||||
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX)
|
||||
if(OpenMP_CXX_FOUND)
|
||||
target_link_libraries(${testname} OpenMP::OpenMP_CXX)
|
||||
endif()
|
||||
install(TARGETS ${testname} DESTINATION "bin/dynamic_programming")
|
||||
|
||||
endforeach( testsourcefile ${APP_SOURCES} )
|
||||
@@ -24,6 +24,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for `assert`
|
||||
#include <cstdint> /// for `std::uint32_t`
|
||||
#include <iostream> /// for IO operations
|
||||
#include <string> /// for `std::string` library
|
||||
#include <vector> /// for `std::vector` STL library
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <limits.h>
|
||||
#include <climits>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -13,13 +14,13 @@ class Edge {
|
||||
class Graph {
|
||||
public:
|
||||
int vertexNum, edgeNum;
|
||||
Edge *edges;
|
||||
std::vector<Edge> edges;
|
||||
|
||||
// Constructs a graph with V vertices and E edges
|
||||
Graph(int V, int E) {
|
||||
this->vertexNum = V;
|
||||
this->edgeNum = E;
|
||||
this->edges = (Edge *)malloc(E * sizeof(Edge));
|
||||
this->edges.reserve(E);
|
||||
}
|
||||
|
||||
// Adds the given edge to the graph
|
||||
@@ -36,7 +37,7 @@ class Graph {
|
||||
};
|
||||
|
||||
// Utility function to print distances
|
||||
void print(int dist[], int V) {
|
||||
void print(const std::vector<int>& dist, int V) {
|
||||
cout << "\nVertex Distance" << endl;
|
||||
for (int i = 0; i < V; i++) {
|
||||
if (dist[i] != INT_MAX)
|
||||
@@ -52,7 +53,8 @@ void print(int dist[], int V) {
|
||||
void BellmanFord(Graph graph, int src) {
|
||||
int V = graph.vertexNum;
|
||||
int E = graph.edgeNum;
|
||||
int dist[V];
|
||||
std::vector<int> dist;
|
||||
dist.reserve(E);
|
||||
|
||||
// Initialize distances array as INF for all except source
|
||||
// Intialize source as zero
|
||||
|
||||
@@ -9,11 +9,12 @@
|
||||
https://oeis.org/A000108/
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for std::uint64_t
|
||||
#include <cstdlib> /// for std::size_t
|
||||
#include <numeric> /// for std::transform_reduce
|
||||
#include <vector> /// for std::vector
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for std::uint64_t
|
||||
#include <cstdlib> /// for std::size_t
|
||||
#include <functional> /// for std::plus & std::multiplies
|
||||
#include <numeric> /// for std::transform_reduce
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
/**
|
||||
* @brief computes and caches Catalan numbers
|
||||
@@ -24,7 +25,7 @@ class catalan_numbers {
|
||||
|
||||
value_type compute_next() {
|
||||
return std::transform_reduce(known.begin(), known.end(), known.rbegin(),
|
||||
static_cast<value_type>(), std::plus<>(),
|
||||
static_cast<value_type>(0), std::plus<>(),
|
||||
std::multiplies<>());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#include <climits>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
// Function to find the Minimum number of coins required to get Sum S
|
||||
int findMinCoins(int arr[], int n, int N) {
|
||||
// dp[i] = no of coins required to get a total of i
|
||||
int dp[N + 1];
|
||||
std::vector<int> dp(N + 1);
|
||||
|
||||
// 0 coins are needed for 0 sum
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <climits>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
/**
|
||||
* @namespace dynamic_programming
|
||||
@@ -70,8 +71,8 @@ int maxProfitByCuttingRod(const std::array<int, T> &price, const uint64_t &n) {
|
||||
*/
|
||||
static void test() {
|
||||
// Test 1
|
||||
const int16_t n1 = 8; // size of rod
|
||||
std::array<int32_t, n1> price1 = {1,2,4,6,8,45,21,9}; // price array
|
||||
const int16_t n1 = 8; // size of rod
|
||||
std::array<int32_t, n1> price1 = {1, 2, 4, 6, 8, 45, 21, 9}; // price array
|
||||
const int64_t max_profit1 =
|
||||
dynamic_programming::cut_rod::maxProfitByCuttingRod(price1, n1);
|
||||
const int64_t expected_max_profit1 = 47;
|
||||
@@ -86,15 +87,15 @@ static void test() {
|
||||
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
||||
41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
|
||||
|
||||
const int64_t max_profit2=
|
||||
const int64_t max_profit2 =
|
||||
dynamic_programming::cut_rod::maxProfitByCuttingRod(price2, n2);
|
||||
const int32_t expected_max_profit2 = 90;
|
||||
assert(max_profit2 == expected_max_profit2);
|
||||
std::cout << "Maximum profit with " << n2 << " inch road is " << max_profit2
|
||||
<< std::endl;
|
||||
// Test 3
|
||||
const int16_t n3 = 5; // size of rod
|
||||
std::array<int32_t, n3> price3 = {2,9,17,23,45}; // price array
|
||||
// Test 3
|
||||
const int16_t n3 = 5; // size of rod
|
||||
std::array<int32_t, n3> price3 = {2, 9, 17, 23, 45}; // price array
|
||||
const int64_t max_profit3 =
|
||||
dynamic_programming::cut_rod::maxProfitByCuttingRod(price3, n3);
|
||||
const int64_t expected_max_profit3 = 45;
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
int min(int x, int y, int z) { return min(min(x, y), z); }
|
||||
@@ -46,7 +47,7 @@ int editDist(string str1, string str2, int m, int n) {
|
||||
*/
|
||||
int editDistDP(string str1, string str2, int m, int n) {
|
||||
// Create Table for SubProblems
|
||||
int dp[m + 1][n + 1];
|
||||
std::vector<std::vector<int> > dp(m + 1, std::vector<int>(n + 1));
|
||||
|
||||
// Fill d[][] in bottom up manner
|
||||
for (int i = 0; i <= m; i++) {
|
||||
|
||||
@@ -4,10 +4,13 @@
|
||||
|
||||
#include <climits>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int eggDrop(int n, int k) {
|
||||
int eggFloor[n + 1][k + 1];
|
||||
std::vector<std::vector<int> > eggFloor(n + 1, std::vector<int>(k + 1));
|
||||
|
||||
int result;
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <climits>
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
@@ -24,7 +25,9 @@ class Graph {
|
||||
}
|
||||
|
||||
~Graph() {
|
||||
for (int i = 0; i < vertexNum; i++) delete[] edges[i];
|
||||
for (int i = 0; i < vertexNum; i++) {
|
||||
delete[] edges[i];
|
||||
}
|
||||
delete[] edges;
|
||||
}
|
||||
|
||||
@@ -35,7 +38,7 @@ class Graph {
|
||||
};
|
||||
|
||||
// Utility function to print distances
|
||||
void print(int dist[], int V) {
|
||||
void print(const std::vector<int>& dist, int V) {
|
||||
cout << "\nThe Distance matrix for Floyd - Warshall" << endl;
|
||||
for (int i = 0; i < V; i++) {
|
||||
for (int j = 0; j < V; j++) {
|
||||
@@ -52,8 +55,8 @@ void print(int dist[], int V) {
|
||||
// The main function that finds the shortest path from a vertex
|
||||
// to all other vertices using Floyd-Warshall Algorithm.
|
||||
void FloydWarshall(Graph graph) {
|
||||
int V = graph.vertexNum;
|
||||
int dist[V][V];
|
||||
std::size_t V = graph.vertexNum;
|
||||
std::vector<std::vector<int> > dist(V, std::vector<int>(V));
|
||||
|
||||
// Initialise distance array
|
||||
for (int i = 0; i < V; i++)
|
||||
@@ -76,7 +79,7 @@ void FloydWarshall(Graph graph) {
|
||||
dist[i][j] = dist[i][k] + dist[k][j];
|
||||
|
||||
// Convert 2d array to 1d array for print
|
||||
int dist1d[V * V];
|
||||
std::vector<int> dist1d(V * V);
|
||||
for (int i = 0; i < V; i++)
|
||||
for (int j = 0; j < V; j++) dist1d[i * V + j] = dist[i][j];
|
||||
|
||||
|
||||
@@ -11,9 +11,9 @@
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <climits> /// for std::max
|
||||
#include <cstdint> /// for std::uint32_t
|
||||
#include <iostream> /// for io operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
/**
|
||||
* @namespace dynamic_programming
|
||||
* @brief Dynamic Programming algorithms
|
||||
|
||||
@@ -1,29 +1,74 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Implementation of [Kadane
|
||||
* Algorithm](https://en.wikipedia.org/wiki/Kadane%27s_algorithm)
|
||||
*
|
||||
* @details
|
||||
* Kadane algorithm is used to find the maximum sum subarray in an array and
|
||||
* maximum sum subarray problem is the task of finding a contiguous subarray
|
||||
* with the largest sum
|
||||
*
|
||||
* ### Algorithm
|
||||
* The simple idea of the algorithm is to search for all positive
|
||||
* contiguous segments of the array and keep track of maximum sum contiguous
|
||||
* segment among all positive segments(curr_sum is used for this)
|
||||
* Each time we get a positive sum we compare it with max_sum and update max_sum
|
||||
* if it is greater than curr_sum
|
||||
*
|
||||
* @author [Ayush Singh](https://github.com/ayush523)
|
||||
*/
|
||||
#include <array>
|
||||
#include <climits>
|
||||
#include <iostream>
|
||||
|
||||
int maxSubArraySum(int a[], int size) {
|
||||
int max_so_far = INT_MIN, max_ending_here = 0;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
max_ending_here = max_ending_here + a[i];
|
||||
if (max_so_far < max_ending_here)
|
||||
max_so_far = max_ending_here;
|
||||
|
||||
if (max_ending_here < 0)
|
||||
max_ending_here = 0;
|
||||
/**
|
||||
* @namespace dynamic_programming
|
||||
* @brief Dynamic Programming algorithms
|
||||
*/
|
||||
namespace dynamic_programming {
|
||||
/**
|
||||
* @namespace kadane
|
||||
* @brief Functions for
|
||||
* [Kadane](https://en.wikipedia.org/wiki/Kadane%27s_algorithm) algorithm.
|
||||
*/
|
||||
namespace kadane {
|
||||
/**
|
||||
* @brief maxSubArray function is used to calculate the maximum sum subarray
|
||||
* and returns the value of maximum sum which is stored in the variable max_sum
|
||||
* @tparam N number of array size
|
||||
* @param n array where numbers are saved
|
||||
* @returns the value of maximum subarray sum
|
||||
*/
|
||||
template <size_t N>
|
||||
int maxSubArray(const std::array<int, N> &n) {
|
||||
int curr_sum =
|
||||
0; // declaring a variable named as curr_sum and initialized it to 0
|
||||
int max_sum = INT_MIN; // Initialized max_sum to INT_MIN
|
||||
for (int i : n) { // for loop to iterate over the elements of the array
|
||||
curr_sum += n[i];
|
||||
max_sum = std::max(max_sum, curr_sum); // getting the maximum value
|
||||
curr_sum = std::max(curr_sum, 0); // updating the value of curr_sum
|
||||
}
|
||||
return max_so_far;
|
||||
return max_sum; // returning the value of max_sum
|
||||
}
|
||||
} // namespace kadane
|
||||
} // namespace dynamic_programming
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main() {
|
||||
int n, i;
|
||||
std::cout << "Enter the number of elements \n";
|
||||
std::cin >> n;
|
||||
int a[n]; // NOLINT
|
||||
for (i = 0; i < n; i++) {
|
||||
std::cin >> a[i];
|
||||
const int N = 5;
|
||||
std::array<int, N> n{}; // declaring array
|
||||
// taking values of elements from user
|
||||
for (int i = 0; i < n.size(); i++) {
|
||||
std::cout << "Enter value of n[" << i << "]"
|
||||
<< "\n";
|
||||
std::cin >> n[i];
|
||||
}
|
||||
int max_sum = maxSubArraySum(a, n);
|
||||
std::cout << "Maximum contiguous sum is " << max_sum;
|
||||
int max_sum = dynamic_programming::kadane::maxSubArray<N>(
|
||||
n); // calling maxSubArray function
|
||||
std::cout << "Maximum subarray sum is " << max_sum; // Printing the answer
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Implementation of [Kadane
|
||||
* Algorithm](https://en.wikipedia.org/wiki/Kadane%27s_algorithm)
|
||||
*
|
||||
* @details
|
||||
* Kadane algorithm is used to find the maximum sum subarray in an array and
|
||||
* maximum sum subarray problem is the task of finding a contiguous subarray
|
||||
* with the largest sum
|
||||
*
|
||||
* ### Algorithm
|
||||
* The simple idea of the algorithm is to search for all positive
|
||||
* contiguous segments of the array and keep track of maximum sum contiguous
|
||||
* segment among all positive segments(curr_sum is used for this)
|
||||
* Each time we get a positive sum we compare it with max_sum and update max_sum
|
||||
* if it is greater than curr_sum
|
||||
*
|
||||
* @author [Ayush Singh](https://github.com/ayush523)
|
||||
*/
|
||||
#include <array>
|
||||
#include <climits>
|
||||
#include <iostream>
|
||||
/**
|
||||
* @namespace dynamic_programming
|
||||
* @brief Dynamic Programming algorithms
|
||||
*/
|
||||
namespace dynamic_programming {
|
||||
/**
|
||||
* @namespace kadane
|
||||
* @brief Functions for
|
||||
* [Kadane](https://en.wikipedia.org/wiki/Kadane%27s_algorithm) algorithm.
|
||||
*/
|
||||
namespace kadane {
|
||||
/**
|
||||
* @brief maxSubArray function is used to calculate the maximum sum subarray
|
||||
* and returns the value of maximum sum which is stored in the variable max_sum
|
||||
* @tparam N number of array size
|
||||
* @param n array where numbers are saved
|
||||
* @returns the value of maximum subarray sum
|
||||
*/
|
||||
template <size_t N>
|
||||
int maxSubArray(const std::array<int, N> &n) {
|
||||
int curr_sum =
|
||||
0; // declaring a variable named as curr_sum and initialized it to 0
|
||||
int max_sum = INT_MIN; // Initialized max_sum to INT_MIN
|
||||
for (int i : n) { // for loop to iterate over the elements of the array
|
||||
curr_sum += n[i];
|
||||
max_sum = std::max(max_sum, curr_sum); // getting the maximum value
|
||||
curr_sum = std::max(curr_sum, 0); // updating the value of curr_sum
|
||||
}
|
||||
return max_sum; // returning the value of max_sum
|
||||
}
|
||||
} // namespace kadane
|
||||
} // namespace dynamic_programming
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main() {
|
||||
const int N = 5;
|
||||
std::array<int, N> n{}; // declaring array
|
||||
// taking values of elements from user
|
||||
for (int i = 0; i < n.size(); i++) {
|
||||
std::cout << "Enter value of n[" << i << "]"
|
||||
<< "\n";
|
||||
std::cin >> n[i];
|
||||
}
|
||||
int max_sum = dynamic_programming::kadane::maxSubArray<N>(
|
||||
n); // calling maxSubArray function
|
||||
std::cout << "Maximum subarray sum is " << max_sum; // Printing the answer
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
// Longest common subsequence - Dynamic Programming
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
void Print(int trace[20][20], int m, int n, string a) {
|
||||
@@ -18,7 +19,7 @@ void Print(int trace[20][20], int m, int n, string a) {
|
||||
|
||||
int lcs(string a, string b) {
|
||||
int m = a.length(), n = b.length();
|
||||
int res[m + 1][n + 1];
|
||||
std::vector<std::vector<int> > res(m + 1, std::vector<int>(n + 1));
|
||||
int trace[20][20];
|
||||
|
||||
// fills up the arrays with zeros.
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <climits> /// for std::max
|
||||
#include <cstdint> /// for std::uint64_t
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
@@ -3,9 +3,11 @@
|
||||
// tested on : https://cses.fi/problemset/task/1145/
|
||||
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
int LIS(int arr[], int n) {
|
||||
int LIS(const std::vector<int>& arr, int n) {
|
||||
set<int> active; // The current built LIS.
|
||||
active.insert(arr[0]);
|
||||
// Loop through every element.
|
||||
@@ -31,7 +33,7 @@ int main(int argc, char const* argv[]) {
|
||||
int n;
|
||||
cout << "Enter size of array: ";
|
||||
cin >> n;
|
||||
int a[n];
|
||||
std::vector<int> a(n);
|
||||
cout << "Enter array elements: ";
|
||||
for (int i = 0; i < n; ++i) {
|
||||
cin >> a[i];
|
||||
@@ -1,6 +1,8 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Implementation of [Minimum Edit Distance](https://en.wikipedia.org/wiki/Edit_distance) using Dynamic Programing
|
||||
* @brief Implementation of [Minimum Edit
|
||||
* Distance](https://en.wikipedia.org/wiki/Edit_distance) using Dynamic
|
||||
* Programing
|
||||
*
|
||||
* @details
|
||||
*
|
||||
@@ -32,9 +34,11 @@
|
||||
* @author [Nirjas Jakilim](github.com/nirzak)
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for IO operations
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for std::uint64_t
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
/**
|
||||
* @namespace dynamic_programming
|
||||
* @brief Dynamic Programming algorithms
|
||||
@@ -44,7 +48,8 @@ namespace dynamic_programming {
|
||||
|
||||
/**
|
||||
* @namespace Minimum Edit Distance
|
||||
* @brief Implementation of [Minimum Edit Distance](https://en.wikipedia.org/wiki/Edit_distance) algorithm
|
||||
* @brief Implementation of [Minimum Edit
|
||||
* Distance](https://en.wikipedia.org/wiki/Edit_distance) algorithm
|
||||
*/
|
||||
|
||||
namespace minimum_edit_distance {
|
||||
@@ -61,15 +66,14 @@ namespace minimum_edit_distance {
|
||||
* @returns z if `z` is the minimum value
|
||||
*/
|
||||
uint64_t min(uint64_t x, uint64_t y, uint64_t z) {
|
||||
if (x <= y && x <= z) {
|
||||
return x; /// returns x, if x is the minimum value
|
||||
}
|
||||
if (y <= x && y <= z) {
|
||||
return y; /// returns y, if y is the minimum value
|
||||
}
|
||||
else {
|
||||
return z; /// returns z if z is the minimum value
|
||||
}
|
||||
if (x <= y && x <= z) {
|
||||
return x; /// returns x, if x is the minimum value
|
||||
}
|
||||
if (y <= x && y <= z) {
|
||||
return y; /// returns y, if y is the minimum value
|
||||
} else {
|
||||
return z; /// returns z if z is the minimum value
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -85,42 +89,48 @@ uint64_t min(uint64_t x, uint64_t y, uint64_t z) {
|
||||
* @returns dp[m][n] the minimum cost of operations
|
||||
* needed to convert str1 to str2
|
||||
*/
|
||||
uint64_t editDistDP(std::string str1, std::string str2, uint64_t m, uint64_t n) {
|
||||
/// Create a table to store results of subproblems
|
||||
std::vector<std::vector<uint64_t>>dp(m+1, std::vector<uint64_t>(n+1)); /// creasting 2D vector dp to store the results of subproblems
|
||||
uint64_t editDistDP(std::string str1, std::string str2, uint64_t m,
|
||||
uint64_t n) {
|
||||
/// Create a table to store results of subproblems
|
||||
std::vector<std::vector<uint64_t>> dp(
|
||||
m + 1,
|
||||
std::vector<uint64_t>(
|
||||
n +
|
||||
1)); /// creasting 2D vector dp to store the results of subproblems
|
||||
|
||||
/// Fill d[][] in bottom up manner
|
||||
for (uint64_t i = 0; i <= m; i++) {
|
||||
for (uint64_t j = 0; j <= n; j++) {
|
||||
/// If first string is empty, only option is to
|
||||
/// insert all characters of second string
|
||||
if (i == 0) {
|
||||
dp[i][j] = j; /// Minimum operations = j
|
||||
}
|
||||
/// Fill d[][] in bottom up manner
|
||||
for (uint64_t i = 0; i <= m; i++) {
|
||||
for (uint64_t j = 0; j <= n; j++) {
|
||||
/// If first string is empty, only option is to
|
||||
/// insert all characters of second string
|
||||
if (i == 0) {
|
||||
dp[i][j] = j; /// Minimum operations = j
|
||||
}
|
||||
|
||||
/// If second string is empty, only option is to
|
||||
/// remove all characters of second string
|
||||
else if (j == 0) {
|
||||
dp[i][j] = i; /// Minimum operations = i
|
||||
}
|
||||
/// If second string is empty, only option is to
|
||||
/// remove all characters of second string
|
||||
else if (j == 0) {
|
||||
dp[i][j] = i; /// Minimum operations = i
|
||||
}
|
||||
|
||||
/// If last characters are same, ignore last char
|
||||
/// and recur for remaining string
|
||||
else if (str1[i - 1] == str2[j - 1]) {
|
||||
dp[i][j] = dp[i - 1][j - 1];
|
||||
}
|
||||
/// If last characters are same, ignore last char
|
||||
/// and recur for remaining string
|
||||
else if (str1[i - 1] == str2[j - 1]) {
|
||||
dp[i][j] = dp[i - 1][j - 1];
|
||||
}
|
||||
|
||||
/// If the last character is different, consider all
|
||||
/// possibilities and find the minimum
|
||||
else {
|
||||
dp[i][j] = 1 + min(dp[i][j - 1], // Insert
|
||||
dp[i - 1][j], // Remove
|
||||
dp[i - 1][j - 1]); // Replace
|
||||
}
|
||||
/// If the last character is different, consider all
|
||||
/// possibilities and find the minimum
|
||||
else {
|
||||
dp[i][j] = 1 + min(dp[i][j - 1], // Insert
|
||||
dp[i - 1][j], // Remove
|
||||
dp[i - 1][j - 1]); // Replace
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dp[m][n]; /// returning the minimum cost of operations needed to convert str1 to str2
|
||||
return dp[m][n]; /// returning the minimum cost of operations needed to
|
||||
/// convert str1 to str2
|
||||
}
|
||||
} // namespace minimum_edit_distance
|
||||
} // namespace dynamic_programming
|
||||
@@ -130,25 +140,28 @@ uint64_t editDistDP(std::string str1, std::string str2, uint64_t m, uint64_t n)
|
||||
* @returns void
|
||||
*/
|
||||
static void test() {
|
||||
// 1st test
|
||||
std::string str1 = "INTENTION"; // Sample input of 1st string
|
||||
std::string str2 = "EXECUTION"; // Sample input of 2nd string
|
||||
uint64_t expected_output1 = 5; // Expected minimum cost
|
||||
uint64_t output1 = dynamic_programming::minimum_edit_distance::editDistDP(
|
||||
str1, str2, str1.length(), str2.length()); // calling the editDistDP function and storing the result on output1
|
||||
assert(output1 == expected_output1); // comparing the output with the expected output
|
||||
std::cout << "Minimum Number of Operations Required: " << output1
|
||||
<< std::endl;
|
||||
// 1st test
|
||||
std::string str1 = "INTENTION"; // Sample input of 1st string
|
||||
std::string str2 = "EXECUTION"; // Sample input of 2nd string
|
||||
uint64_t expected_output1 = 5; // Expected minimum cost
|
||||
uint64_t output1 = dynamic_programming::minimum_edit_distance::editDistDP(
|
||||
str1, str2, str1.length(),
|
||||
str2.length()); // calling the editDistDP function and storing the
|
||||
// result on output1
|
||||
assert(output1 ==
|
||||
expected_output1); // comparing the output with the expected output
|
||||
std::cout << "Minimum Number of Operations Required: " << output1
|
||||
<< std::endl;
|
||||
|
||||
// 2nd test
|
||||
std::string str3 = "SATURDAY";
|
||||
std::string str4 = "SUNDAY";
|
||||
uint64_t expected_output2 = 3;
|
||||
uint64_t output2 = dynamic_programming::minimum_edit_distance::editDistDP(
|
||||
str3, str4, str3.length(), str4.length());
|
||||
assert(output2 == expected_output2);
|
||||
std::cout << "Minimum Number of Operations Required: " << output2
|
||||
<< std::endl;
|
||||
// 2nd test
|
||||
std::string str3 = "SATURDAY";
|
||||
std::string str4 = "SUNDAY";
|
||||
uint64_t expected_output2 = 3;
|
||||
uint64_t output2 = dynamic_programming::minimum_edit_distance::editDistDP(
|
||||
str3, str4, str3.length(), str4.length());
|
||||
assert(output2 == expected_output2);
|
||||
std::cout << "Minimum Number of Operations Required: " << output2
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,6 +171,6 @@ static void test() {
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main(int argc, char *argv[]) {
|
||||
test(); // run self-test implementations
|
||||
return 0;
|
||||
test(); // run self-test implementations
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -28,10 +28,10 @@
|
||||
*
|
||||
*******************************************************************************/
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for std::uint64_t
|
||||
#include <iostream> /// for IO Operations
|
||||
#include <numeric> /// for std::accumulate
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
/******************************************************************************
|
||||
* @namespace dp
|
||||
* @brief Dynamic programming algorithms
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Implementation of the Unbounded 0/1 Knapsack Problem
|
||||
*
|
||||
* @details
|
||||
* The Unbounded 0/1 Knapsack problem allows taking unlimited quantities of each item.
|
||||
* The goal is to maximize the total value without exceeding the given knapsack capacity.
|
||||
* Unlike the 0/1 knapsack, where each item can be taken only once, in this variation,
|
||||
* any item can be picked any number of times as long as the total weight stays within
|
||||
* the knapsack's capacity.
|
||||
*
|
||||
* Given a set of N items, each with a weight and a value, represented by the arrays
|
||||
* `wt` and `val` respectively, and a knapsack with a weight limit W, the task is to
|
||||
* fill the knapsack to maximize the total value.
|
||||
*
|
||||
* @note weight and value of items is greater than zero
|
||||
* @details
|
||||
* The Unbounded 0/1 Knapsack problem allows taking unlimited quantities of each
|
||||
* item. The goal is to maximize the total value without exceeding the given
|
||||
* knapsack capacity. Unlike the 0/1 knapsack, where each item can be taken only
|
||||
* once, in this variation, any item can be picked any number of times as long
|
||||
* as the total weight stays within the knapsack's capacity.
|
||||
*
|
||||
* Given a set of N items, each with a weight and a value, represented by the
|
||||
* arrays `wt` and `val` respectively, and a knapsack with a weight limit W, the
|
||||
* task is to fill the knapsack to maximize the total value.
|
||||
*
|
||||
* @note weight and value of items is greater than zero
|
||||
*
|
||||
* ### Algorithm
|
||||
* The approach uses dynamic programming to build a solution iteratively.
|
||||
* A 2D array is used for memoization to store intermediate results, allowing
|
||||
* The approach uses dynamic programming to build a solution iteratively.
|
||||
* A 2D array is used for memoization to store intermediate results, allowing
|
||||
* the function to avoid redundant calculations.
|
||||
*
|
||||
*
|
||||
* @author [Sanskruti Yeole](https://github.com/yeolesanskruti)
|
||||
* @see dynamic_programming/0_1_knapsack.cpp
|
||||
*/
|
||||
|
||||
#include <cassert> // For using assert function to validate test cases
|
||||
#include <cstdint> // For fixed-width integer types like std::uint16_t
|
||||
#include <iostream> // Standard input-output stream
|
||||
#include <vector> // Standard library for using dynamic arrays (vectors)
|
||||
#include <cassert> // For using assert function to validate test cases
|
||||
#include <cstdint> // For fixed-width integer types like std::uint16_t
|
||||
#include <vector> // Standard library for using dynamic arrays (vectors)
|
||||
|
||||
/**
|
||||
* @namespace dynamic_programming
|
||||
@@ -42,7 +42,7 @@ namespace dynamic_programming {
|
||||
namespace unbounded_knapsack {
|
||||
|
||||
/**
|
||||
* @brief Recursive function to calculate the maximum value obtainable using
|
||||
* @brief Recursive function to calculate the maximum value obtainable using
|
||||
* an unbounded knapsack approach.
|
||||
*
|
||||
* @param i Current index in the value and weight vectors.
|
||||
@@ -52,27 +52,33 @@ namespace unbounded_knapsack {
|
||||
* @param wt Vector of weights corresponding to the items.
|
||||
* @note "wt" data type can be changed according to the size of the input.
|
||||
* @param dp 2D vector for memoization to avoid redundant calculations.
|
||||
* @return The maximum value that can be obtained for the given index and capacity.
|
||||
* @return The maximum value that can be obtained for the given index and
|
||||
* capacity.
|
||||
*/
|
||||
std::uint16_t KnapSackFilling(std::uint16_t i, std::uint16_t W,
|
||||
const std::vector<std::uint16_t>& val,
|
||||
const std::vector<std::uint16_t>& wt,
|
||||
std::vector<std::vector<int>>& dp) {
|
||||
std::uint16_t KnapSackFilling(std::uint16_t i, std::uint16_t W,
|
||||
const std::vector<std::uint16_t>& val,
|
||||
const std::vector<std::uint16_t>& wt,
|
||||
std::vector<std::vector<int>>& dp) {
|
||||
if (i == 0) {
|
||||
if (wt[0] <= W) {
|
||||
return (W / wt[0]) * val[0]; // Take as many of the first item as possible
|
||||
return (W / wt[0]) *
|
||||
val[0]; // Take as many of the first item as possible
|
||||
} else {
|
||||
return 0; // Can't take the first item
|
||||
return 0; // Can't take the first item
|
||||
}
|
||||
}
|
||||
if (dp[i][W] != -1) return dp[i][W]; // Return result if available
|
||||
if (dp[i][W] != -1)
|
||||
return dp[i][W]; // Return result if available
|
||||
|
||||
int nottake = KnapSackFilling(i - 1, W, val, wt, dp); // Value without taking item i
|
||||
int nottake =
|
||||
KnapSackFilling(i - 1, W, val, wt, dp); // Value without taking item i
|
||||
int take = 0;
|
||||
if (W >= wt[i]) {
|
||||
take = val[i] + KnapSackFilling(i, W - wt[i], val, wt, dp); // Value taking item i
|
||||
take = val[i] + KnapSackFilling(i, W - wt[i], val, wt,
|
||||
dp); // Value taking item i
|
||||
}
|
||||
return dp[i][W] = std::max(take, nottake); // Store and return the maximum value
|
||||
return dp[i][W] =
|
||||
std::max(take, nottake); // Store and return the maximum value
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,17 +90,19 @@ std::uint16_t KnapSackFilling(std::uint16_t i, std::uint16_t W,
|
||||
* @param wt Vector of weights corresponding to the items.
|
||||
* @return The maximum value that can be obtained for the given capacity.
|
||||
*/
|
||||
std::uint16_t unboundedKnapsack(std::uint16_t N, std::uint16_t W,
|
||||
const std::vector<std::uint16_t>& val,
|
||||
const std::vector<std::uint16_t>& wt) {
|
||||
if(N==0)return 0; // Expect 0 since no items
|
||||
std::vector<std::vector<int>> dp(N, std::vector<int>(W + 1, -1)); // Initialize memoization table
|
||||
return KnapSackFilling(N - 1, W, val, wt, dp); // Start the calculation
|
||||
std::uint16_t unboundedKnapsack(std::uint16_t N, std::uint16_t W,
|
||||
const std::vector<std::uint16_t>& val,
|
||||
const std::vector<std::uint16_t>& wt) {
|
||||
if (N == 0)
|
||||
return 0; // Expect 0 since no items
|
||||
std::vector<std::vector<int>> dp(
|
||||
N, std::vector<int>(W + 1, -1)); // Initialize memoization table
|
||||
return KnapSackFilling(N - 1, W, val, wt, dp); // Start the calculation
|
||||
}
|
||||
|
||||
} // unbounded_knapsack
|
||||
} // namespace unbounded_knapsack
|
||||
|
||||
} // dynamic_programming
|
||||
} // namespace dynamic_programming
|
||||
|
||||
/**
|
||||
* @brief self test implementation
|
||||
@@ -102,42 +110,57 @@ std::uint16_t unboundedKnapsack(std::uint16_t N, std::uint16_t W,
|
||||
*/
|
||||
static void tests() {
|
||||
// Test Case 1
|
||||
std::uint16_t N1 = 4; // Number of items
|
||||
std::vector<std::uint16_t> wt1 = {1, 3, 4, 5}; // Weights of the items
|
||||
std::vector<std::uint16_t> val1 = {6, 1, 7, 7}; // Values of the items
|
||||
std::uint16_t W1 = 8; // Maximum capacity of the knapsack
|
||||
std::uint16_t N1 = 4; // Number of items
|
||||
std::vector<std::uint16_t> wt1 = {1, 3, 4, 5}; // Weights of the items
|
||||
std::vector<std::uint16_t> val1 = {6, 1, 7, 7}; // Values of the items
|
||||
std::uint16_t W1 = 8; // Maximum capacity of the knapsack
|
||||
// Test the function and assert the expected output
|
||||
assert(unboundedKnapsack(N1, W1, val1, wt1) == 48);
|
||||
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N1, W1, val1, wt1) << std::endl;
|
||||
assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(
|
||||
N1, W1, val1, wt1) == 48);
|
||||
std::cout << "Maximum Knapsack value "
|
||||
<< dynamic_programming::unbounded_knapsack::unboundedKnapsack(
|
||||
N1, W1, val1, wt1)
|
||||
<< std::endl;
|
||||
|
||||
// Test Case 2
|
||||
std::uint16_t N2 = 3; // Number of items
|
||||
std::vector<std::uint16_t> wt2 = {10, 20, 30}; // Weights of the items
|
||||
std::vector<std::uint16_t> val2 = {60, 100, 120}; // Values of the items
|
||||
std::uint16_t W2 = 5; // Maximum capacity of the knapsack
|
||||
std::uint16_t N2 = 3; // Number of items
|
||||
std::vector<std::uint16_t> wt2 = {10, 20, 30}; // Weights of the items
|
||||
std::vector<std::uint16_t> val2 = {60, 100, 120}; // Values of the items
|
||||
std::uint16_t W2 = 5; // Maximum capacity of the knapsack
|
||||
// Test the function and assert the expected output
|
||||
assert(unboundedKnapsack(N2, W2, val2, wt2) == 0);
|
||||
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N2, W2, val2, wt2) << std::endl;
|
||||
assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(
|
||||
N2, W2, val2, wt2) == 0);
|
||||
std::cout << "Maximum Knapsack value "
|
||||
<< dynamic_programming::unbounded_knapsack::unboundedKnapsack(
|
||||
N2, W2, val2, wt2)
|
||||
<< std::endl;
|
||||
|
||||
// Test Case 3
|
||||
std::uint16_t N3 = 3; // Number of items
|
||||
std::vector<std::uint16_t> wt3 = {2, 4, 6}; // Weights of the items
|
||||
std::vector<std::uint16_t> val3 = {5, 11, 13};// Values of the items
|
||||
std::uint16_t W3 = 27;// Maximum capacity of the knapsack
|
||||
std::uint16_t N3 = 3; // Number of items
|
||||
std::vector<std::uint16_t> wt3 = {2, 4, 6}; // Weights of the items
|
||||
std::vector<std::uint16_t> val3 = {5, 11, 13}; // Values of the items
|
||||
std::uint16_t W3 = 27; // Maximum capacity of the knapsack
|
||||
// Test the function and assert the expected output
|
||||
assert(unboundedKnapsack(N3, W3, val3, wt3) == 27);
|
||||
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N3, W3, val3, wt3) << std::endl;
|
||||
assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(
|
||||
N3, W3, val3, wt3) == 27);
|
||||
std::cout << "Maximum Knapsack value "
|
||||
<< dynamic_programming::unbounded_knapsack::unboundedKnapsack(
|
||||
N3, W3, val3, wt3)
|
||||
<< std::endl;
|
||||
|
||||
// Test Case 4
|
||||
std::uint16_t N4 = 0; // Number of items
|
||||
std::vector<std::uint16_t> wt4 = {}; // Weights of the items
|
||||
std::vector<std::uint16_t> val4 = {}; // Values of the items
|
||||
std::uint16_t W4 = 10; // Maximum capacity of the knapsack
|
||||
assert(unboundedKnapsack(N4, W4, val4, wt4) == 0);
|
||||
std::cout << "Maximum Knapsack value for empty arrays: " << unboundedKnapsack(N4, W4, val4, wt4) << std::endl;
|
||||
|
||||
std::cout << "All test cases passed!" << std::endl;
|
||||
std::uint16_t N4 = 0; // Number of items
|
||||
std::vector<std::uint16_t> wt4 = {}; // Weights of the items
|
||||
std::vector<std::uint16_t> val4 = {}; // Values of the items
|
||||
std::uint16_t W4 = 10; // Maximum capacity of the knapsack
|
||||
assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(
|
||||
N4, W4, val4, wt4) == 0);
|
||||
std::cout << "Maximum Knapsack value for empty arrays: "
|
||||
<< dynamic_programming::unbounded_knapsack::unboundedKnapsack(
|
||||
N4, W4, val4, wt4)
|
||||
<< std::endl;
|
||||
|
||||
std::cout << "All test cases passed!" << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -145,7 +168,6 @@ static void tests() {
|
||||
* @return 0 on successful exit
|
||||
*/
|
||||
int main() {
|
||||
tests(); // Run self test implementation
|
||||
tests(); // Run self test implementation
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -105,7 +105,7 @@ bool check(const std::string &s, const std::unordered_set<std::string> &strSet,
|
||||
// if the prefix till current position is present in the dictionary
|
||||
// and the remaining substring can also be segmented legally, then
|
||||
// set solution at position pos in the memo, and return true
|
||||
if (exists(wordTillNow, strSet) and check(s, strSet, i + 1, dp)) {
|
||||
if (exists(wordTillNow, strSet) && check(s, strSet, i + 1, dp)) {
|
||||
dp->at(pos) = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
18
greedy_algorithms/CMakeLists.txt
Normal file
18
greedy_algorithms/CMakeLists.txt
Normal file
@@ -0,0 +1,18 @@
|
||||
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
||||
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
||||
# automatically.
|
||||
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
|
||||
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
|
||||
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
|
||||
foreach( testsourcefile ${APP_SOURCES} )
|
||||
# I used a simple string replace, to cut off .cpp.
|
||||
string( REPLACE ".cpp" "" testname ${testsourcefile} )
|
||||
add_executable( ${testname} ${testsourcefile} )
|
||||
|
||||
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX)
|
||||
if(OpenMP_CXX_FOUND)
|
||||
target_link_libraries(${testname} OpenMP::OpenMP_CXX)
|
||||
endif()
|
||||
install(TARGETS ${testname} DESTINATION "bin/greedy_algorithms")
|
||||
|
||||
endforeach( testsourcefile ${APP_SOURCES} )
|
||||
@@ -44,7 +44,7 @@ int main() {
|
||||
cout << "\n Enter the number of Items : ";
|
||||
int n;
|
||||
cin >> n;
|
||||
Item itemArray[n];
|
||||
Item *itemArray = new Item[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << "\nEnter the weight and profit of item " << i + 1 << " : ";
|
||||
cin >> itemArray[i].weight;
|
||||
@@ -73,6 +73,6 @@ int main() {
|
||||
}
|
||||
|
||||
cout << "\nMax Profit : " << maxProfit;
|
||||
|
||||
delete[] itemArray;
|
||||
return 0;
|
||||
}
|
||||
|
||||
15
operations_on_datastructures/CMakeLists.txt
Normal file
15
operations_on_datastructures/CMakeLists.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
||||
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
||||
# automatically.
|
||||
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
|
||||
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
|
||||
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
|
||||
foreach( testsourcefile ${APP_SOURCES} )
|
||||
# I used a simple string replace, to cut off .cpp.
|
||||
string( REPLACE ".cpp" "" testname ${testsourcefile} )
|
||||
add_executable( ${testname} ${testsourcefile} )
|
||||
|
||||
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX)
|
||||
install(TARGETS ${testname} DESTINATION "bin/operations_on_datastructures")
|
||||
|
||||
endforeach( testsourcefile ${APP_SOURCES} )
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
|
||||
int queue[10];
|
||||
int front = 0;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief [Trie datastructure](https://iq.opengenus.org/autocomplete-using-trie-data-structure/)
|
||||
* @brief [Trie
|
||||
* datastructure](https://iq.opengenus.org/autocomplete-using-trie-data-structure/)
|
||||
* with search variants
|
||||
* @details
|
||||
* This provides multiple variants of search functions
|
||||
@@ -12,6 +13,7 @@
|
||||
#include <algorithm> /// for std::count
|
||||
#include <cassert> /// for assert
|
||||
#include <cctype> /// for tolower
|
||||
#include <cstdint> /// for std::uint32_t
|
||||
#include <cstring> /// for string operations
|
||||
#include <iostream> /// for IO Operations
|
||||
#include <queue> /// for std::priority_queue
|
||||
@@ -23,7 +25,8 @@
|
||||
namespace operations_on_datastructures {
|
||||
/**
|
||||
* @namespace trie_operations
|
||||
* @brief Functions for [Trie datastructure](https://iq.opengenus.org/autocomplete-using-trie-data-structure/)
|
||||
* @brief Functions for [Trie
|
||||
* datastructure](https://iq.opengenus.org/autocomplete-using-trie-data-structure/)
|
||||
* implementation
|
||||
*/
|
||||
namespace trie_operations {
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
void fastinput(int *number) {
|
||||
// variable to indicate sign of input integer
|
||||
bool negative = false;
|
||||
register int c;
|
||||
int c;
|
||||
*number = 0;
|
||||
|
||||
// extract current character from buffer
|
||||
|
||||
18
range_queries/CMakeLists.txt
Normal file
18
range_queries/CMakeLists.txt
Normal file
@@ -0,0 +1,18 @@
|
||||
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
||||
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
||||
# automatically.
|
||||
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
|
||||
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
|
||||
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
|
||||
foreach( testsourcefile ${APP_SOURCES} )
|
||||
# I used a simple string replace, to cut off .cpp.
|
||||
string( REPLACE ".cpp" "" testname ${testsourcefile} )
|
||||
add_executable( ${testname} ${testsourcefile} )
|
||||
|
||||
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX)
|
||||
if(OpenMP_CXX_FOUND)
|
||||
target_link_libraries(${testname} OpenMP::OpenMP_CXX)
|
||||
endif()
|
||||
install(TARGETS ${testname} DESTINATION "bin/range_queries")
|
||||
|
||||
endforeach( testsourcefile ${APP_SOURCES} )
|
||||
@@ -1,4 +1,7 @@
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
const int N = 1e6 + 5;
|
||||
int a[N], bucket[N], cnt[N];
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cmath> /// for log2
|
||||
#include <cstdint> /// for std::uint64_t
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
Reference in New Issue
Block a user