style: format code

This commit is contained in:
yanglbme
2019-08-21 10:10:08 +08:00
parent abc0d365de
commit 69ddc9fc52
101 changed files with 3154 additions and 2984 deletions

View File

@@ -1,6 +1,6 @@
//0-1 Knapsack problem - Dynamic programming
//#include <bits/stdc++.h>
#include<iostream>
#include <iostream>
using namespace std;
//void Print(int res[20][20], int i, int j, int capacity)
@@ -15,7 +15,7 @@ using namespace std;
// {
// cout<<i<<" ";
// }
//
//
// Print(res, i-1, j-1, capacity-i);
// }
// else if(res[i-1][j]>res[i][j-1])
@@ -28,43 +28,44 @@ using namespace std;
// }
//}
int Knapsack(int capacity,int n,int weight[],int value[]){
int Knapsack(int capacity, int n, int weight[], int value[])
{
int res[20][20];
for (int i = 0; i < n+1; ++i)
for (int i = 0; i < n + 1; ++i)
{
for (int j = 0; j < capacity+1; ++j)
for (int j = 0; j < capacity + 1; ++j)
{
if(i==0||j==0)
if (i == 0 || j == 0)
res[i][j] = 0;
else if(weight[i-1]<=j)
res[i][j] = max(value[i-1]+res[i-1][j-weight[i-1]], res[i-1][j]);
else if (weight[i - 1] <= j)
res[i][j] = max(value[i - 1] + res[i - 1][j - weight[i - 1]], res[i - 1][j]);
else
res[i][j] = res[i-1][j];
res[i][j] = res[i - 1][j];
}
}
// Print(res, n, capacity, capacity);
// cout<<"\n";
// Print(res, n, capacity, capacity);
// cout<<"\n";
return res[n][capacity];
}
int main()
{
int n;
cout<<"Enter number of items: ";
cin>>n;
cout << "Enter number of items: ";
cin >> n;
int weight[n], value[n];
cout<<"Enter weights: ";
cout << "Enter weights: ";
for (int i = 0; i < n; ++i)
{
cin>>weight[i];
cin >> weight[i];
}
cout<<"Enter values: ";
cout << "Enter values: ";
for (int i = 0; i < n; ++i)
{
cin>>value[i];
cin >> value[i];
}
int capacity;
cout<<"Enter capacity: ";
cin>>capacity;
cout<<Knapsack(capacity,n,weight,value);
cout << "Enter capacity: ";
cin >> capacity;
cout << Knapsack(capacity, n, weight, value);
return 0;
}

View File

@@ -1,20 +1,20 @@
//program to check whether a number is an armstrong number or not
#include<iostream.h>
#include<Math.h>
#include <iostream.h>
#include <Math.h>
int main()
{
int n,k,d,s=0;
cout<<"Enter a number:";
cin>>n;
k=n;
while(k!=0)
int n, k, d, s = 0;
cout << "Enter a number:";
cin >> n;
k = n;
while (k != 0)
{
d=k%10;
s+=(int)pow(d,3);
k/=10;
d = k % 10;
s += (int)pow(d, 3);
k /= 10;
}
if(s==n)
cout<<n<<"is an armstrong number";
if (s == n)
cout << n << "is an armstrong number";
else
cout<<n<<"is not an armstrong number";
cout << n << "is not an armstrong number";
}

View File

@@ -1,116 +1,128 @@
#include<iostream>
#include<limits.h>
#include <iostream>
#include <limits.h>
using namespace std;
//Wrapper class for storing an edge
class Edge{
public: int src,dst,weight;
class Edge
{
public:
int src, dst, weight;
};
//Wrapper class for storing a graph
class Graph{
public:
int vertexNum,edgeNum;
Edge* edges;
class Graph
{
public:
int vertexNum, edgeNum;
Edge *edges;
//Constructs a graph with V vertices and E edges
Graph(int V,int E){
Graph(int V, int E)
{
this->vertexNum = V;
this->edgeNum = E;
this->edges =(Edge*) malloc(E * sizeof(Edge));
this->edges = (Edge *)malloc(E * sizeof(Edge));
}
//Adds the given edge to the graph
void addEdge(int src, int dst, int weight){
//Adds the given edge to the graph
void addEdge(int src, int dst, int weight)
{
static int edgeInd = 0;
if(edgeInd < this->edgeNum){
if (edgeInd < this->edgeNum)
{
Edge newEdge;
newEdge.src = src;
newEdge.dst = dst;
newEdge.weight = weight;
this->edges[edgeInd++] = newEdge;
}
}
};
//Utility function to print distances
void print(int dist[], int V){
cout<<"\nVertex Distance"<<endl;
for(int i = 0; i < V; i++){
if( dist[i] != INT_MAX)
cout<<i<<"\t"<<dist[i]<<endl;
void print(int dist[], int V)
{
cout << "\nVertex Distance" << endl;
for (int i = 0; i < V; i++)
{
if (dist[i] != INT_MAX)
cout << i << "\t" << dist[i] << endl;
else
cout<<i<<"\tINF"<<endl;
cout << i << "\tINF" << endl;
}
}
//The main function that finds the shortest path from given source
//to all other vertices using Bellman-Ford.It also detects negative
//weight cycle
void BellmanFord(Graph graph, int src){
void BellmanFord(Graph graph, int src)
{
int V = graph.vertexNum;
int E = graph.edgeNum;
int dist[V];
//Initialize distances array as INF for all except source
//Intialize source as zero
for(int i=0; i<V; i++)
for (int i = 0; i < V; i++)
dist[i] = INT_MAX;
dist[src] = 0;
//Calculate shortest path distance from source to all edges
//A path can contain maximum (|V|-1) edges
for(int i=0; i<=V-1; i++)
for(int j = 0; j<E; j++){
for (int i = 0; i <= V - 1; i++)
for (int j = 0; j < E; j++)
{
int u = graph.edges[j].src;
int v = graph.edges[j].dst;
int w = graph.edges[j].weight;
if(dist[u]!=INT_MAX && dist[u] + w < dist[v])
if (dist[u] != INT_MAX && dist[u] + w < dist[v])
dist[v] = dist[u] + w;
}
//Iterate inner loop once more to check for negative cycle
for(int j = 0; j<E; j++){
for (int j = 0; j < E; j++)
{
int u = graph.edges[j].src;
int v = graph.edges[j].dst;
int w = graph.edges[j].weight;
if(dist[u]!=INT_MAX && dist[u] + w < dist[v]){
cout<<"Graph contains negative weight cycle. Hence, shortest distance not guaranteed."<<endl;
if (dist[u] != INT_MAX && dist[u] + w < dist[v])
{
cout << "Graph contains negative weight cycle. Hence, shortest distance not guaranteed." << endl;
return;
}
}
print(dist, V);
return;
}
//Driver Function
int main(){
int V,E,gsrc;
int src,dst,weight;
cout<<"Enter number of vertices: ";
cin>>V;
cout<<"Enter number of edges: ";
cin>>E;
Graph G(V,E);
for(int i=0; i<E; i++){
cout<<"\nEdge "<<i+1<<"\nEnter source: ";
cin>>src;
cout<<"Enter destination: ";
cin>>dst;
cout<<"Enter weight: ";
cin>>weight;
int main()
{
int V, E, gsrc;
int src, dst, weight;
cout << "Enter number of vertices: ";
cin >> V;
cout << "Enter number of edges: ";
cin >> E;
Graph G(V, E);
for (int i = 0; i < E; i++)
{
cout << "\nEdge " << i + 1 << "\nEnter source: ";
cin >> src;
cout << "Enter destination: ";
cin >> dst;
cout << "Enter weight: ";
cin >> weight;
G.addEdge(src, dst, weight);
}
cout<<"\nEnter source: ";
cin>>gsrc;
BellmanFord(G,gsrc);
cout << "\nEnter source: ";
cin >> gsrc;
BellmanFord(G, gsrc);
return 0;
}

View File

@@ -13,7 +13,7 @@ int *cat; // global array to hold catalan numbers
unsigned long int catalan_dp(int n)
{
/** Using the tabulation technique in dynamic programming,
/** Using the tabulation technique in dynamic programming,
this function computes the first `n+1` Catalan numbers
Parameter
@@ -26,39 +26,39 @@ unsigned long int catalan_dp(int n)
*/
// By definition, the first two Catalan numbers are 1
cat[0] = cat[1] = 1;
cat[0] = cat[1] = 1;
// Compute the remaining numbers from index 2 to index n, using tabulation
for (int i = 2; i <= n; i++)
{
cat[i] = 0;
for (int j = 0; j < i; j++)
cat[i] += cat[j] * cat[i-j-1]; // applying the definition here
}
for (int i = 2; i <= n; i++)
{
cat[i] = 0;
for (int j = 0; j < i; j++)
cat[i] += cat[j] * cat[i - j - 1]; // applying the definition here
}
// Return the result
return cat[n];
return cat[n];
}
int main(int argc, char *argv[])
{
int n;
cout << "Enter n: ";
cin >> n;
int n;
cout << "Enter n: ";
cin >> n;
cat = new int[n+1];
cat = new int[n + 1];
cout << "Catalan numbers from 0 to " << n << " are:\n";
for (int i = 0; i <= n; i++)
cout << "Catalan numbers from 0 to " << n << " are:\n";
for (int i = 0; i <= n; i++)
{
cout << "catalan (" << i << ") = " << catalan_dp(i) << endl;
// NOTE: Since `cat` is a global array, calling `catalan_dp`
cout << "catalan (" << i << ") = " << catalan_dp(i) << endl;
// NOTE: Since `cat` is a global array, calling `catalan_dp`
// repeatedly will not recompute the the values already computed
// as in case of pre-computed values, the array will simply return them,
// as in case of pre-computed values, the array will simply return them,
// instead of recomputing them.
}
return 0;
return 0;
}
/** Sample Test Case:

View File

@@ -1,5 +1,5 @@
#include <iostream>
#include<climits>
#include <climits>
using namespace std;
// Function to find the Minimum number of coins required to get Sum S
@@ -7,11 +7,10 @@ int findMinCoins(int arr[], int n, int N)
{
// dp[i] = no of coins required to get a total of i
int dp[N + 1];
// 0 coins are needed for 0 sum
dp[0] = 0;
// 0 coins are needed for 0 sum
dp[0] = 0;
for (int i = 1; i <= N; i++)
{
@@ -39,13 +38,13 @@ int findMinCoins(int arr[], int n, int N)
int main()
{
// No of Coins We Have
int arr[] = { 1, 2, 3, 4 };
int arr[] = {1, 2, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);
// Total Change Required
int N = 15;
cout << "Minimum Number of Coins Required "<< findMinCoins(arr, n, N) << "\n";
cout << "Minimum Number of Coins Required " << findMinCoins(arr, n, N) << "\n";
return 0;
}

View File

@@ -5,25 +5,24 @@ the pieces.*/
#include <bits/stdc++.h>
using namespace std;
int cutrod(int p[],int n)
int cutrod(int p[], int n)
{
int r[n+1];
r[0]=0;
for(int j=0;j<n;j++)
int r[n + 1];
r[0] = 0;
for (int j = 0; j < n; j++)
{
int q=INT_MIN;
for(int i=0;i<=j;i++)
int q = INT_MIN;
for (int i = 0; i <= j; i++)
{
q = max(q,p[i]+r[j-i]);
q = max(q, p[i] + r[j - i]);
}
r[j+1]=q;
r[j + 1] = q;
}
return r[n];
}
int main()
{
int price[]={1,5,8,9,10,17,17,20,24,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};
cout<<cutrod(price,30);
int price[] = {1, 5, 8, 9, 10, 17, 17, 20, 24, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
cout << cutrod(price, 30);
return 0;
}

View File

@@ -15,8 +15,9 @@
#include <string>
using namespace std;
int min(int x, int y, int z){
return min(min(x,y), z);
int min(int x, int y, int z)
{
return min(min(x, y), z);
}
/* A Naive recursive C++ program to find
@@ -24,62 +25,68 @@ int min(int x, int y, int z){
* str1 to str2.
* O(3^m)
*/
int editDist(string str1, string str2, int m, int n) {
if(m == 0) return n;
if(n == 0) return m;
//If last characters are same then continue
//for the rest of them.
if(str1[m-1] == str2[n-1])
return editDist(str1, str2, m-1, n-1);
int editDist(string str1, string str2, int m, int n)
{
if (m == 0)
return n;
if (n == 0)
return m;
//If last not same, then 3 possibilities
//a.Insert b.Remove c. Replace
//Get min of three and continue for rest.
return 1 + min ( editDist(str1, str2, m, n-1),
editDist(str1, str2, m-1, n),
editDist(str1, str2, m-1, n-1)
);
//If last characters are same then continue
//for the rest of them.
if (str1[m - 1] == str2[n - 1])
return editDist(str1, str2, m - 1, n - 1);
//If last not same, then 3 possibilities
//a.Insert b.Remove c. Replace
//Get min of three and continue for rest.
return 1 + min(editDist(str1, str2, m, n - 1),
editDist(str1, str2, m - 1, n),
editDist(str1, str2, m - 1, n - 1));
}
/* A DP based program
* O(m x n)
*/
int editDistDP(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];
int dp[m + 1][n + 1];
//Fill d[][] in bottom up manner
for(int i=0; i<=m; i++) {
for(int j=0; j<=n; j++) {
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
//If str1 empty. Then add all of str2
if(i==0)
if (i == 0)
dp[i][j] = j;
//If str2 empty. Then add all of str1
else if(j==0)
//If str2 empty. Then add all of str1
else if (j == 0)
dp[i][j] = i;
//If character same. Recur for remaining
else if(str1[i-1] == str2[j-1])
dp[i][j] = dp[i-1][j-1];
//If character same. Recur for remaining
else if (str1[i - 1] == str2[j - 1])
dp[i][j] = dp[i - 1][j - 1];
else
dp[i][j] = 1 + min(dp[i][j-1],//Insert
dp[i-1][j],//Remove
dp[i-1][j-1]//Replace
);
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];
}
int main() {
int main()
{
string str1 = "sunday";
string str2 = "saturday";
cout << editDist(str1, str2, str1.length(), str2.length()) << endl;
cout << editDistDP(str1, str2, str1.length(), str2.length()) << endl;

View File

@@ -6,40 +6,47 @@
#include <climits>
using namespace std;
int eggDrop(int n, int k) {
int eggFloor[n+1][k+1];
int eggDrop(int n, int k)
{
int eggFloor[n + 1][k + 1];
int result;
for(int i=1; i<=n; i++) {
for (int i = 1; i <= n; i++)
{
eggFloor[i][1] = 1; //n eggs..1 Floor
eggFloor[i][0] = 0; //n eggs..0 Floor
}
// Only one egg available
for(int j=1; j<=k ; j++) {
for (int j = 1; j <= k; j++)
{
eggFloor[1][j] = j;
}
for(int i=2; i<=n; i++) {
for(int j=2; j<=k; j++) {
for (int i = 2; i <= n; i++)
{
for (int j = 2; j <= k; j++)
{
eggFloor[i][j] = INT_MAX;
for(int x=1; x<=j; x++) {
for (int x = 1; x <= j; x++)
{
// 1+max(eggBreak[one less egg, lower floors],
// eggDoesntBreak[same # of eggs, upper floors]);
result = 1 + max(eggFloor[i-1][x-1], eggFloor[i][j-x]);
if(result < eggFloor[i][j])
result = 1 + max(eggFloor[i - 1][x - 1], eggFloor[i][j - x]);
if (result < eggFloor[i][j])
eggFloor[i][j] = result;
}
}
}
return eggFloor[n][k];
}
int main() {
int main()
{
int n, k;
cout << "Enter number of eggs and floors: ";
cin>>n>>k;
cin >> n >> k;
cout << "Minimum number of trials in worst case: " << eggDrop(n, k) << endl;
return 0;
}

View File

@@ -1,19 +1,22 @@
#include <bits/stdc++.h>
using namespace std;
int fib(int n){
int res[n+1];
res[0] = 0; res[1] = 1;
for(int i=2;i<=n;i++){
res[i] = res[i-1] + res[i-2];
int fib(int n)
{
int res[n + 1];
res[0] = 0;
res[1] = 1;
for (int i = 2; i <= n; i++)
{
res[i] = res[i - 1] + res[i - 2];
}
return res[n];
}
int main(int argc, char const *argv[])
{
int n;
cout<<"Enter n: ";
cin>>n;
cout<<"Fibonacci number is ";
cout<<fib(n)<<endl;
cout << "Enter n: ";
cin >> n;
cout << "Fibonacci number is ";
cout << fib(n) << endl;
return 0;
}

View File

@@ -1,24 +1,26 @@
#include <bits/stdc++.h>
using namespace std;
int arr[1000000];
int fib(int n){
if(arr[n]==-1){
if(n<=1)
int fib(int n)
{
if (arr[n] == -1)
{
if (n <= 1)
arr[n] = n;
else
arr[n] = fib(n-1) + fib(n-2);
arr[n] = fib(n - 1) + fib(n - 2);
}
return arr[n];
}
int main(int argc, char const *argv[])
{
int n;
cout<<"Enter n: ";
cin>>n;
for (int i = 0; i < n+1; ++i)
cout << "Enter n: ";
cin >> n;
for (int i = 0; i < n + 1; ++i)
{
arr[i] = -1;
}
cout<<"Fibonacci number is "<<fib(n)<<endl;
cout << "Fibonacci number is " << fib(n) << endl;
return 0;
}

View File

@@ -1,103 +1,109 @@
#include<iostream>
#include<limits.h>
#include<string.h>
#include <iostream>
#include <limits.h>
#include <string.h>
using namespace std;
//Wrapper class for storing a graph
class Graph{
public:
class Graph
{
public:
int vertexNum;
int** edges;
int **edges;
//Constructs a graph with V vertices and E edges
Graph(int V){
Graph(int V)
{
this->vertexNum = V;
this->edges =(int**) malloc(V * sizeof(int*));
for(int i=0; i<V; i++){
this->edges[i] = (int*) malloc(V * sizeof(int));
for(int j=0; j<V; j++)
this->edges = (int **)malloc(V * sizeof(int *));
for (int i = 0; i < V; i++)
{
this->edges[i] = (int *)malloc(V * sizeof(int));
for (int j = 0; j < V; j++)
this->edges[i][j] = INT_MAX;
this->edges[i][i] = 0;
}
}
}
//Adds the given edge to the graph
void addEdge(int src, int dst, int weight){
//Adds the given edge to the graph
void addEdge(int src, int dst, int weight)
{
this->edges[src][dst] = weight;
}
};
//Utility function to print distances
void print(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++){
void print(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++)
{
if(dist[i*V+j] != INT_MAX)
cout<<dist[i*V+j]<<"\t";
else
cout<<"INF"<<"\t";
if (dist[i * V + j] != INT_MAX)
cout << dist[i * V + j] << "\t";
else
cout << "INF"
<< "\t";
}
cout<<endl;
cout << endl;
}
}
//The main function that finds the shortest path from a vertex
//to all other vertices using Floyd-Warshall Algorithm.
void FloydWarshall(Graph graph){
void FloydWarshall(Graph graph)
{
int V = graph.vertexNum;
int dist[V][V];
//Initialise distance array
for(int i=0; i<V; i++)
for(int j=0; j<V; j++)
dist[i][j] = graph.edges[i][j];
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
dist[i][j] = graph.edges[i][j];
//Calculate distances
for(int k=0; k<V; k++)
//Choose an intermediate vertex
for (int k = 0; k < V; k++)
//Choose an intermediate vertex
for(int i=0; i<V; i++)
//Choose a source vertex for given intermediate
for (int i = 0; i < V; i++)
//Choose a source vertex for given intermediate
for(int j=0; j<V; j++)
//Choose a destination vertex for above source vertex
for (int j = 0; j < V; j++)
//Choose a destination vertex for above source vertex
if(dist[i][k] != INT_MAX && dist[k][j] != INT_MAX && dist[i][k] + dist[k][j] < dist[i][j])
//If the distance through intermediate vertex is less than direct edge then update value in distance array
if (dist[i][k] != INT_MAX && dist[k][j] != INT_MAX && dist[i][k] + dist[k][j] < dist[i][j])
//If the distance through intermediate vertex is less than direct edge then update value in distance array
dist[i][j] = dist[i][k] + dist[k][j];
//Convert 2d array to 1d array for print
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];
print(dist1d,V);
}
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];
print(dist1d, V);
}
//Driver Function
int main(){
int V,E;
int src,dst,weight;
cout<<"Enter number of vertices: ";
cin>>V;
cout<<"Enter number of edges: ";
cin>>E;
int main()
{
int V, E;
int src, dst, weight;
cout << "Enter number of vertices: ";
cin >> V;
cout << "Enter number of edges: ";
cin >> E;
Graph G(V);
for(int i=0; i<E; i++){
cout<<"\nEdge "<<i+1<<"\nEnter source: ";
cin>>src;
cout<<"Enter destination: ";
cin>>dst;
cout<<"Enter weight: ";
cin>>weight;
for (int i = 0; i < E; i++)
{
cout << "\nEdge " << i + 1 << "\nEnter source: ";
cin >> src;
cout << "Enter destination: ";
cin >> dst;
cout << "Enter weight: ";
cin >> weight;
G.addEdge(src, dst, weight);
}
FloydWarshall(G);

View File

@@ -4,69 +4,67 @@ using namespace std;
void Print(int trace[20][20], int m, int n, string a)
{
if (m==0 || n==0)
if (m == 0 || n == 0)
{
return;
}
if (trace[m][n]==1)
if (trace[m][n] == 1)
{
Print(trace, m-1, n-1, a);
cout<<a[m-1];
Print(trace, m - 1, n - 1, a);
cout << a[m - 1];
}
else if(trace[m][n]==2)
else if (trace[m][n] == 2)
{
Print(trace, m-1,n, a);
Print(trace, m - 1, n, a);
}
else if (trace[m][n]==3)
else if (trace[m][n] == 3)
{
Print(trace, m,n-1, a);
Print(trace, m, n - 1, a);
}
}
int lcs(string a, string b)
{
int m = a.length(), n = b.length();
int res[m+1][n+1];
int res[m + 1][n + 1];
int trace[20][20];
// fills up the arrays with zeros.
for (int i = 0; i < m+1; i++)
for (int i = 0; i < m + 1; i++)
{
for (int j = 0; j < n+1; j++)
for (int j = 0; j < n + 1; j++)
{
res[i][j] = 0;
trace[i][j] = 0;
}
}
for (int i = 0; i < m+1; ++i)
for (int i = 0; i < m + 1; ++i)
{
for (int j = 0; j < n+1; ++j)
for (int j = 0; j < n + 1; ++j)
{
if(i==0||j==0)
if (i == 0 || j == 0)
{
res[i][j] = 0;
trace[i][j]=0;
trace[i][j] = 0;
}
else if(a[i-1]==b[j-1])
else if (a[i - 1] == b[j - 1])
{
res[i][j] = 1 + res[i-1][j-1];
trace[i][j]=1; // 1 means trace the matrix in upper left diagonal direction.
res[i][j] = 1 + res[i - 1][j - 1];
trace[i][j] = 1; // 1 means trace the matrix in upper left diagonal direction.
}
else
{
if (res[i-1][j]>res[i][j-1])
if (res[i - 1][j] > res[i][j - 1])
{
res[i][j]=res[i-1][j];
trace[i][j]=2; // 2 means trace the matrix in upwards direction.
res[i][j] = res[i - 1][j];
trace[i][j] = 2; // 2 means trace the matrix in upwards direction.
}
else
{
res[i][j]=res[i][j-1];
trace[i][j]=3; // means trace the matrix in left direction.
res[i][j] = res[i][j - 1];
trace[i][j] = 3; // means trace the matrix in left direction.
}
}
}
@@ -75,13 +73,10 @@ int lcs(string a, string b)
return res[m][n];
}
int main()
{
string a,b;
cin>>a>>b;
cout<<lcs(a,b);
string a, b;
cin >> a >> b;
cout << lcs(a, b);
return 0;
}

View File

@@ -1,7 +1,8 @@
//Program to calculate length of longest increasing subsequence in an array
#include <bits/stdc++.h>
using namespace std;
int LIS(int a[],int n){
int LIS(int a[], int n)
{
int lis[n];
for (int i = 0; i < n; ++i)
{
@@ -11,28 +12,28 @@ int LIS(int a[],int n){
{
for (int j = 0; j < i; ++j)
{
if(a[i]>a[j] && lis[i]<lis[j]+1)
if (a[i] > a[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
}
}
int res = 0;
for (int i = 0; i < n; ++i)
{
res = max(res,lis[i]);
res = max(res, lis[i]);
}
return res;
}
int main(int argc, char const *argv[])
{
int n;
cout<<"Enter size of array: ";
cin>>n;
cout << "Enter size of array: ";
cin >> n;
int a[n];
cout<<"Enter array elements: ";
cout << "Enter array elements: ";
for (int i = 0; i < n; ++i)
{
cin>>a[i];
cin >> a[i];
}
cout<<LIS(a,n)<<endl;
cout << LIS(a, n) << endl;
return 0;
}

View File

@@ -18,7 +18,7 @@ int MatrixChainMultiplication(int dim[], int i, int j)
// needed to compute the matrix M[i+1]...M[j] = M[i..j]
int min = INT_MAX;
// if dp[i][j] is not calculated (calculate it!!)
// if dp[i][j] is not calculated (calculate it!!)
if (dp[i][j] == 0)
{
@@ -34,7 +34,7 @@ int MatrixChainMultiplication(int dim[], int i, int j)
cost += MatrixChainMultiplication(dim, k, j);
// cost to multiply two (i x k) and (k x j) matrix
cost += dim[i] * dim[k] * dim[j];
cost += dim[i] * dim[k] * dim[j];
if (cost < min)
min = cost; // store the minimum cost
@@ -51,7 +51,7 @@ int main()
{
// Matrix i has Dimensions dim[i-1] & dim[i] for i=1..n
// input is 10 x 30 matrix, 30 x 5 matrix, 5 x 60 matrix
int dim[] = { 10, 30, 5, 60 };
int dim[] = {10, 30, 5, 60};
int n = sizeof(dim) / sizeof(dim[0]);
// Function Calling: MatrixChainMultiplications(dimensions_array, starting, ending);