-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ajay
committed
Feb 2, 2013
0 parents
commit 54d05ba
Showing
14 changed files
with
1,046 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* @desc: this program reads in an integer n from stdin. It prints | ||
* all combinations of balanced n pairs of parentheses, i.e., | ||
* n '(' and n ')'. | ||
* | ||
* A recursive function is used to compute all strings having | ||
* balanced parentheses. The logic is simple. | ||
* | ||
* @author: Ajay | ||
* @time: Monday 17 December 2012, 8:36 PM | ||
* | ||
* | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
#define MAX_LEN 100 | ||
|
||
void print_paran(int pos, int n, int open, int close); | ||
|
||
int main(void) { | ||
int n; | ||
scanf("%d", &n); | ||
print_paran(0, n, 0, 0); | ||
return 0; | ||
} | ||
|
||
void print_paran(int pos, int n, int open, int close) { | ||
static char s[MAX_LEN]; | ||
static int count = 0; | ||
if(close == n) { | ||
s[pos] = '\0'; | ||
printf("%2d. %s\n", ++count, s); | ||
return; | ||
} | ||
if(open < n) { | ||
s[pos] = '('; | ||
print_paran(pos + 1, n, open + 1, close); | ||
} | ||
if(open > close) { | ||
s[pos] = ')'; | ||
print_paran(pos + 1, n, open, close + 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* | ||
* | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
int main(void) { | ||
int len, a[10] = {0}; | ||
len = *(&a + 1) - a; // len = 1[&a] - a | ||
printf("%d\n", len); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* | ||
* Desc: This program reads in two strings s, t from stdin and | ||
* computes minimum-cost way to edit s into t using | ||
* three operations of insert, delete and substitute. | ||
* | ||
* The problem satisfies the two conditions for dynamic | ||
* programming - optimal substructure and overlapping subproblems. | ||
* We define edit[i][j] the cost of editing the first i chars of | ||
* s into first j chars of t. Then edit[i][j] is the minimum of the the below | ||
* three: | ||
* edit[i-1][j-1] + sub, (sub = 0 if s[i] == t[j] else s = 1) | ||
* for matching or substitution resp. | ||
* | ||
* edit[i][j-1] + 1, inserting t[j] into s[i+1] so now we have to | ||
* edit the first i chars of s into first j-1 chars of t. | ||
* | ||
* edit[i-1] + 1, deleting s[i] so that now we have to edit the first | ||
* i-1 chars of s into the first j chars of t. | ||
* | ||
* edit[m][n] stores the minimum-cost way to edit the string s into the | ||
* string t, where m = strlen(s) and n = strlen(t); | ||
* | ||
* | ||
* Author: Ajay | ||
* | ||
* Time: Tuesday, 27 November 2012, 12:41 AM | ||
* | ||
* | ||
* | ||
* | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#define MAX 1000 | ||
|
||
int min(int a, int b, int c); | ||
int stringCompare(char *s, char *t); | ||
|
||
int main(void) { | ||
char s[MAX], t[MAX]; | ||
char c; | ||
int i = 0, j = 0; | ||
|
||
while((c = getchar()) != '\n') s[i++] = c; | ||
s[i] = '\0'; | ||
|
||
while((c = getchar()) != '\n') t[j++] = c; | ||
t[j] = '\0'; | ||
|
||
int d = stringCompare(s, t); | ||
printf("%d\n", d); | ||
|
||
return 0; | ||
} | ||
|
||
int stringCompare(char *s, char *t) { | ||
char edit[MAX][MAX]; | ||
|
||
int m = strlen(s); | ||
int n = strlen(t); | ||
|
||
int i, j; | ||
int minCost, sub; | ||
|
||
for(i = 0; i < MAX; i++) edit[i][0] = i; | ||
for(j = 0; j < MAX; j++) edit[0][j] = j; | ||
|
||
for(i = 1; i <= m; i++) { | ||
for(j = 1; j <= n; j++) { | ||
(s[i-1] == t[j-1]) ? (sub = 0) : (sub = 1); | ||
int match = edit[i-1][j-1] + sub; | ||
int insert = edit[i][j-1] + 1; | ||
int delete = edit[i-1][j] +1; | ||
|
||
minCost = min(match, insert, delete); | ||
edit[i][j] = minCost; | ||
} | ||
} | ||
return edit[m][n]; | ||
} | ||
|
||
int min(int a, int b, int c) { | ||
int res = a; | ||
if(res > b) res = b; | ||
if(res > c) res = c; | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* @desc: This program reads in a sequence of integers from stdin | ||
* terminated by the EOF (ctrl+D). It then prints the length | ||
* of the longest increasing subsequence of the input sequence, | ||
* the length of the longest increasing sequence ending at | ||
* each element of the input sequence, and finally the longest | ||
* increasing subsequence itself. | ||
* | ||
* @algo: The algorithm is simple. For the input element a[i], we find | ||
* the longest increasing subsequence containing a[i] as its last | ||
* element. We find this for all the elements in the input sequence. | ||
* Then we select the a[i] for which this length is maximum. If l[i] | ||
* denotes this value, then l[i] = max{l[j] : a[i] >= a[j]}. We also | ||
* store this value of j so that we can construct the actual subsequence. | ||
* The complexity of the program is O(n^2) where n is the length of the | ||
* input sequence. | ||
* | ||
* @author: ajay | ||
* | ||
* @time: Monday, 31st December 2012, 1:48 PM | ||
* | ||
* | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
#define MAX_LEN 100 | ||
|
||
typedef struct { | ||
int prev_index; | ||
int len; | ||
} lis_struct; | ||
|
||
void lis(int a[], int len); | ||
void print_lis(int a[], lis_struct b[], int max_index); | ||
|
||
int main(void) { | ||
int i = 0, a[MAX_LEN]; | ||
int val; | ||
while(scanf("%d", &val) != EOF) a[i++] = val; | ||
lis(a, i); | ||
return 0; | ||
} | ||
|
||
void lis(int a[], int len) { | ||
lis_struct b[MAX_LEN]; | ||
int i, j; | ||
int max_index = 0; | ||
for(i = 0; i < len; i++) { | ||
b[i].len = 1; | ||
b[i].prev_index = -1; | ||
for(j = 0; j < i; j++) { | ||
if(a[j] <= a[i] && b[j].len >= b[i].len) { | ||
b[i].len = b[j].len + 1; | ||
b[i].prev_index = j; | ||
} | ||
} | ||
if(b[i].len > b[max_index].len) | ||
max_index = i; | ||
} | ||
printf("\nthe array b\n"); | ||
for(i = 0; i < len; i++) printf("%d ", b[i].len); | ||
printf("\n%d\n", b[max_index].len); | ||
print_lis(a, b, max_index); | ||
printf("\n"); | ||
} | ||
|
||
void print_lis(int a[], lis_struct b[], int max_index) { | ||
if(b[max_index].prev_index == -1) { | ||
printf("%d ", a[max_index]); | ||
return; | ||
} | ||
print_lis(a, b, b[max_index].prev_index); | ||
printf("%d ", a[max_index]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* | ||
author: Ajay | ||
time: Monday 29 October 2012, 10:43 AM | ||
desc: This program checks if a linked list is a palindrome WITHOUT USING | ||
EXTRA SPACE. It prints 1 in case it is and 0 otherwise. | ||
algo: First, the first node of the second half of the list is found. Then | ||
the second half is reversed and checked with the first half until we | ||
we reach the end of the second half(NULL) or when the equality | ||
condition fails(data of a node from the first half should be equal to | ||
the data of the corresponding node from the second half of the list). | ||
The second half is then reversed back. | ||
* | ||
*/ | ||
|
||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
typedef struct Node { | ||
int data; | ||
struct Node *next; | ||
} Pnode; | ||
|
||
Pnode *createList(void); | ||
Pnode *reverseList(Pnode *head); | ||
Pnode *recursiveRevList(Pnode *newHead, Pnode *oldHead, Pnode *temp); | ||
int isPalindrome(Pnode *head); | ||
|
||
|
||
int main(void) { | ||
Pnode *head = createList(); | ||
int res = isPalindrome(head); | ||
printf("%d \n", res); | ||
return 0; | ||
} | ||
|
||
int isPalindrome(Pnode *head) { | ||
Pnode *m = head, *p = head, *temp = NULL; | ||
while(p != NULL) { | ||
p = p -> next; | ||
if(p != NULL) p = p -> next; | ||
m = m -> next; | ||
} | ||
m = reverseList(m); | ||
temp = m; | ||
p = head; | ||
while(m != NULL) { | ||
if((p -> data) != (m -> data)) { | ||
m = reverseList(temp); | ||
return 0; | ||
} | ||
p = p -> next; | ||
m = m -> next; | ||
} | ||
m = reverseList(temp); | ||
return 1; | ||
} | ||
|
||
Pnode *createList(void) { | ||
char c; | ||
Pnode *head = NULL, *temp, *current; | ||
c = getchar(); | ||
if(c != '\n') { | ||
current = (Pnode *) malloc(sizeof(Pnode)); | ||
current -> data = (int) c; | ||
current -> next = NULL; | ||
} | ||
else return head; | ||
|
||
head = current; | ||
|
||
while((c = getchar()) != '\n') { | ||
temp = (Pnode *) malloc(sizeof(Pnode)); | ||
temp -> data = (int) c; | ||
temp -> next = NULL; | ||
current -> next = temp; | ||
current = temp; | ||
} | ||
return head; | ||
} | ||
|
||
Pnode *recursiveRevList(Pnode *newHead, Pnode *oldHead, Pnode *temp) { | ||
if(oldHead == NULL) return newHead; | ||
temp = oldHead -> next; | ||
oldHead -> next = newHead; | ||
newHead = oldHead; | ||
oldHead = temp; | ||
return recursiveRevList(newHead, oldHead, temp); | ||
} | ||
|
||
Pnode *reverseList(Pnode *head) { | ||
Pnode *newHead = NULL, *temp = NULL; | ||
newHead = recursiveRevList(newHead, head, temp); | ||
return newHead; | ||
} | ||
|
||
void printList(Pnode *head) { | ||
Pnode *node = head; | ||
while(node != NULL) { | ||
printf("%d", node -> data); | ||
node = node -> next; | ||
} | ||
printf("\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
#define MAX 100 | ||
|
||
int lis(int a[], int k); | ||
|
||
int main(void) { | ||
int a[MAX], i = 0; | ||
int val, res; | ||
while(scanf("%d", &val) != EOF) | ||
a[i++] = val; | ||
res = lis(a, i - 1); | ||
printf("\n%d\n", res); | ||
return 0; | ||
} | ||
|
||
int lis(int a[], int k) { | ||
if(k == 0) return 1; | ||
int i, len = 0, max = 0; | ||
for(i = 0; i < k; i++) { | ||
if(a[k] >= a[i]) len = lis(a, i) + 1; | ||
if(len > max) max = len; | ||
} | ||
//printf("%d\n", max); | ||
return max; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* | ||
* | ||
* | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
Oops, something went wrong.