Skip to content

Commit

Permalink
Add dsap dynamic programming problems
Browse files Browse the repository at this point in the history
  • Loading branch information
tranchitam committed Jun 30, 2015
1 parent 0fd0b91 commit 3be8757
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <cstdio>
#include <cmath>
#define MIN -99999999
#define MAX 99999999

/*
10
5 2 3 4 9 10 5 6 7 8
*/
int main(){
int n;
scanf("%d", &n);
int a[n + 2];
a[0] = MIN;
a[n + 1] = MAX;
for(int i = 1; i <= n; i++){
scanf("%d", &a[i]);
}
int t[n];
int L[n + 2];
L[n + 1] = 1;
for(int i = n ; i >=0; i--){
int jmax = n + 1;
for(int j = i + 1; j <= n + 1; j++){
if(a[j] > a[i] && L[j] > L[jmax]){
jmax = j;
}
}
L[i] = L[jmax] + 1;
t[i] = jmax;
}
int i = t[0];
printf("%d\n", L[0] - 2);
while(i != (n + 1)){
printf("a[%d] = %d\n", i - 1, a[i]);
i = t[i];
}
}
43 changes: 43 additions & 0 deletions lightoj.com/1023.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>

#define S "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

using namespace std;

void solve(char a[], bool b[], int n, int k, int i, int &c){
if(c < k){
for(int j = 0; j < n; j++){
if(b[j] == false){
a[i] = S[j];
if(i == n - 1){
c++;
for(int ii = 0; ii < n; ii++){
printf("%c", a[ii]);
}
printf("\n");
}else{
b[j] = true;
solve(a, b, n, k, i + 1, c);
b[j] = false;
}
}
}
}
}

int main(){
int t, n, k;
scanf("%d", &t);
for(int i = 1; i <= t; i++){
scanf("%d %d", &n, &k);
char a[n];
bool b[n];
int c = 0;
memset(b, false, sizeof b);
printf("Case %d:\n", i);
solve(a, b, n, k, 0, c);
}
}

0 comments on commit 3be8757

Please sign in to comment.