Data Structures and Algorithms
Data Structures and Algorithms
B11+B12+B13
Code:
#include<bits/stdc++.h>
using namespace std;
}
};
node * huffman_hidden(string s) {
spq pq;
vector<int>count(256,0);
if( count[i] != 0 )
pq.push(n_node);
while( pq.size() != 1 ) {
}
NAME: VARUN SAPRA
B11+B12+B13
return pq.top();
if(root == NULL)
return;
if(root->data != '\0') {
mp[root->data] = code;
}
int main() {
string s;
std::cin >> s;
string coded;
decode_huff(tree,coded);
return 0;
}
Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct node {
int data;
struct node *left;
struct node *right;
};
void swap_nodes_at_level(struct node *root, int inc, int level, int heigh
t){
struct node *tnode;
if(!root){
return;
}
if(level > height){
return;
}
if(!(level%inc)){
tnode=root->left;
root->left=root->right;
root->right=tnode;
}
swap_nodes_at_level(root->left, inc, level+1, height);
swap_nodes_at_level(root->right, inc, level+1, height);
}
int tail=0;
int head=0;
queue[tail]=root;
tail++;
}
int main() {
NAME: VARUN SAPRA
B11+B12+B13
scanf("%d", &nodes_count);
i=0,index=1;
root_temp=root_perm=create_node(1);
enqueue(q, root_temp);
while(index<=2*nodes_count) {
root_temp=dequeue(q);
scanf("%d", &temp1);
if(temp1 == -1){
} else {
root_temp->left=create_node(temp1);
enqueue(q, root_temp->left);
}
scanf("%d", &temp2);
if(temp2==-1) {
} else {
root_temp->right=create_node(temp2);
enqueue(q, root_temp->right);
}
index=index+2;
NAME: VARUN SAPRA
B11+B12+B13
h = height(root_perm);
scanf("%d", &tc_num);
while(tc_num){
scanf("%d",&inc);
temp=inc;
inorder(root_perm);
printf("\n");
tc_num--;
}
return 0;
}
Code:
vector<int> nodes;
void inOrderTraversal(Node* node) {
if(node == NULL) {
return;
}
inOrderTraversal(node->left);
nodes.push_back(node->data);
inOrderTraversal(node->right);
}
bool checkBST(Node* root) {
inOrderTraversal(root);
for(int i = 1; i < nodes.size(); i++) {
NAME: VARUN SAPRA
B11+B12+B13
Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
NAME: VARUN SAPRA
B11+B12+B13
struct stack {
char stk[MAX_SIZE];
int top;
} s;
void push(char c) {
if (s.top < MAX_SIZE) {
s.stk[++s.top] = c;
}
}
char pop() {
if (s.top > -1) {
return s.stk[s.top--];
} else {
return -1;
}
}
int main() {
int t, length;
char str[1000], c;
scanf("%d\n", &t);
for (int i = 0; i < t; i++) {
s.top = -1;
length = 0;
for (;;) {
c = getchar();
if (c == EOF || c == '\n') {
break;
}
str[length] = c;
length++;
}
printf("%s\n", balancedParentheses(str, length) == 0 ? "NO" : "YE
S");
}
return 0;
}
Code:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int g;
scanf("%d",&g);
for(int a0 = 0; a0 < g; a0++){
int n;
int m;
int x;
scanf("%d %d %d",&n,&m,&x);
int *a = malloc(sizeof(int) * n);
for(int a_i = 0; a_i < n; a_i++){
scanf("%d",&a[a_i]);
}
int *b = malloc(sizeof(int) * m);
for(int b_i = 0; b_i < m; b_i++){
scanf("%d",&b[b_i]);
}
NAME: VARUN SAPRA
B11+B12+B13
int max = 0;
int sum = 0;
int items = 0;
int apos = 0, bpos = 0;
while (sum <= x && apos < n) {
if (sum + a[apos] > x)
break;
sum += a[apos];
apos++;
items++;
}
while (sum <= x && bpos < m) {
if (sum + b[bpos] > x)
break;
sum += b[bpos];
bpos++;
items++;
}
max = items;
while (1) {
if (apos <= 0)
break;
apos--;
sum -= a[apos];
items--;
while (sum <= x && bpos < m) {
if (sum + b[bpos] > x)
break;
sum += b[bpos];
bpos++;
items++;
}
if (items > max)
max = items;
if (bpos == m)
break;
}
printf ("%d\n", max);
}
return 0;
}
NAME: VARUN SAPRA
B11+B12+B13
Code:
#include <stdio.h>
#define SIZ 100000
int m[SIZ+1];
long long st[SIZ];
int main(){
int N,j,ptr;
long long r,h;
scanf("%d",&N);
for(j=0;j<N;j++)scanf("%d",m+j);m[j]=0;
for(r=ptr=j=0;j<=N;j++){
int left=j;
for(;ptr && (h=st[ptr-1]>>32)>m[j];){
int l=st[--ptr]&0xffffffff;
if(r<h*(j-l))r=h*(j-l);
left=l;
}
st[ptr++]=((long long)m[j]<<32)|left;
}
NAME: VARUN SAPRA
B11+B12+B13
printf("%lld\n",r);
return 0;
}
Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
int main() {
FILE * in = stdin;
FILE * out = stdout;
int command;
int k;
char buf[1000000];
NAME: VARUN SAPRA
B11+B12+B13
char ** undos;
int n, i, ptr = 0;
size_t len;
fscanf(in, "%d", &n);
undos = calloc(sizeof(char *), n);
undos[ptr] = malloc(sizeof(char));
strcpy(undos[ptr], "");
for (i = 0; i < n; ++i) {
fscanf(in, "%d", &command);
switch(command) {
case 1:
fscanf(in, "%1000000s\n", buf);
++ptr;
undos[ptr] = malloc(sizeof(char) * (strlen(undos[ptr-1])
+ strlen(buf) + 1));
strcpy(undos[ptr], undos[ptr-1]);
strcat(undos[ptr], buf);
break;
case 2:
fscanf(in, "%d", &k);
++ptr;
undos[ptr] = malloc(sizeof(char) * (strlen(undos[ptr - 1]
) - k + 1));
len = strlen(undos[ptr - 1]);
memcpy(undos[ptr], undos[ptr-1], strlen(undos[ptr - 1]) -
k);
undos[ptr][len - k] = 0;
break;
case 3:
fscanf(in, "%d", &k);
fprintf(out, "%c\n", undos[ptr][k-1]);
break;
case 4:
--ptr;
break;
}
}
return 0;
}
NAME: VARUN SAPRA
B11+B12+B13
Task 8 : Waiter
Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define DSTACK 2
#define OP1 0
#define OP2 1
int sp[3];
int *stack[3];
void initS(int n) {
for (int i=0;i<3;++i) {
stack[i]= (int *) malloc(sizeof(int)*n);
sp[i]=0;
}
}
int popS(int i) {
return stack[i][--sp[i]];
}
void closeS() {
for (int i=0;i<3;++i) free(stack[i]);
}
int emptyS(int i) {
return sp[i]==0;
}
int itemsS(int i) {
return sp[i];
}
int isPrime(int n) {
return 1;
int nextPrime(int n) {
++n;
while(!isPrime(n))
++n;
return n;
}
NAME: VARUN SAPRA
B11+B12+B13
int main() {
int n, q;
int i;
int v;
int ins, outs, prime;
scanf("%d %d",&n,&q);
int items[q];
for (i=0;i<n;++i) {
scanf("%d",&v);
pushS(0,v); // stack 0
}
ins=0;
outs=1;
prime=2;
for(i=0;i<q;++i) {
while(!emptyS(ins)) {
int v= popS(ins);
if (v%prime==0) pushS(DSTACK,v); //stack2
else pushS(outs,v);
}
items[i]=itemsS(DSTACK);
// SWAP stacks
int tmp=ins;
ins=outs;
outs=tmp;
prime=nextPrime(prime);
while(!emptyS(DSTACK)) {
v= popS(DSTACK);
printf("%d\n",v);
NAME: VARUN SAPRA
B11+B12+B13
}
}
while(!emptyS(ins)) {
v= popS(ins);
printf("%d\n",v);
}
closeS();
return 0;
}