Daa Lab Cat
Daa Lab Cat
Problem Statement
Imagine you are in a data science class, and your instructor, Professor Huffman, has tasked you with
writing a program to decode messages encoded using a specific Huffman tree.
The Huffman tree has two types of nodes: internal nodes represented by a "*" character and leaf
nodes containing either "A" or "B". You need to write a program that can take an encoded message
and decode it according to this predefined Huffman tree structure.
Input format :
The input consists of a string of '0's and '1's representing the encoded message, without any space.
Output format :
The output displays the decoded message, which will be a sequence of characters ('A' or 'B').
Code constraints :
Input 1 :
11100011100
Output 1 :
BBBAAABBBAA
CODE:
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
struct HuffmanNode {
char data;
HuffmanNode* left;
HuffmanNode* right;
HuffmanNode(char data) {
this->data = data;
left = nullptr;
right = nullptr;
}
};
int main() {
string encodedMessage;
cin >> encodedMessage;
decodeHuffman(root, encodedMessage);
return 0;
}
2.
Sarah is analyzing the sales performance of her store over a series of consecutive days. She wants to
identify the best-performing period in terms of maximum total sales.
She also needs to calculate the average daily sales and list the sales figures for that period to
understand the contributing factors. Help her to calculate all these values using the divide and
conquer algorithm.
Example
Input:
-3 1 -1 5 6
Output:
11
5.50
[5, 6]
Explanation:
The array is divided into left [-3, 1, -1] and right [5, 6].
The right half [5, 6] has the highest sum (11), so the result is Sum = 11, Average = 5.50, Subarray =
[5,6].
Input format :
The first line of input consists of an integer N, representing the size of the array.
The second line consists of N space-separated integers, representing the sales figures for each day.
Output format :
The first line of output prints the maximum sum of the subarray,
The second line of output prints a double representing the average sales rounded to two decimal
places.
The third line of output prints an array representing the elements in the subarray.
Refer to the sample output for formatting specifications.
Code constraints :
1 ≤ N ≤ 20
Input 1 :
-3 1 -1 5 6
Output 1 :
11
5.50
[5, 6]
Input 2 :
-2 -1 -3 -4
Output 2 :
-1
-1.00
[-1]
CODE:
#include <bits/stdc++.h>
using namespace std;
int main() {
int tt = 1;
while(tt--) {
int n, k;
cin >> n >> k;
vector<int > arr(n);
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
queue<int > Q;
for (int i = 0; i < k; ++i) {
if(arr[i] % 2 == 0) {
Q.push(i);
}
}
if(Q.size()) {
cout << arr[Q.front()] << " ";
if(Q.front() == 0 ){
Q.pop();
}
}
else {
cout << -1 << " ";
}
for (int i = k; i < n; ++i) {
if(arr[i] % 2 == 0) {
Q.push(i);
}
if(Q.size()) {
cout << arr[Q.front()] << " ";
if(Q.front() == i - k + 1) Q.pop();
}
else {
cout << -1 <<" ";
}
}
cout << endl;
}
return 0;
}
3.
John needs to multiply several pairs of large numbers in a single program. He wants to use the
Karatsuba algorithm for efficient computation. Help John to complete the task.
Input format :
The first line contains a single integer n, representing the number of pairs.
The next n lines contain a pair of two integers x and y representing the multiplicands separated by
spaces
Output format :
The output displays n lines containing the product of x and y, rounded to three decimal places
Code constraints :
1 ≤ n ≤ 100
1 ≤ x, y ≤ 1018
Input 1 :
2
12345 67890
98765 43210
Output 1 :
Input 2 :
123456789 987654321
11111 22222
555555555 333333333
Output 2 :
CODE:
#include <stdio.h>
#include <math.h>
int get_size(long);
long b = X / multiplier;
long a = X - (b * multiplier);
long d = Y / multiplier;
long c = Y - (d * multiplier);
int main() {
int n;
scanf("%d", &n);
long x, y;
for (int i = 0; i < n; i++) {
scanf("%ld %ld", &x, &y);
printf("Product of pair %d: %ld\n", i + 1, karatsuba(x, y));
}
return 0;
}
4.
Emma, a software developer, is working on a plagiarism detection tool for an educational platform.
The platform receives large text documents from students, and Emma needs to compare the
submitted papers to check for similarities. She decides to implement an algorithm that finds the
Longest Common Subsequence (LCS) between two documents, which are represented as strings. To
achieve this, Emma needs a program that can efficiently calculate the length of the LCS for these
large document strings.
Help Emma by writing a program that calculates the length of the LCS between two given document
strings
Input format :
The first line of input contains a string representing the first document.
The second line of input contains a string representing the second document.
Output format :
The first line of output displays all the unique combinations of the longest common subsequence.
The last line of output displays the length(counted based on the number of characters in the
subsequence including the spaces between words) of the longest common subsequence.
CODE:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char lcs[MAX]={0};
char *results[MAX]={0};
int count=0;
void backtrack(int i,int j,int idx,int dp[MAX][MAX],char*str1,char*str2){
if(i==0 || j==0){
lcs[idx]='\0';\
for(int k=0;k<count;k++){
if(strcmp(results[k],lcs)==0){
return;
}
}
results[count++]=strdup(lcs);
return;
}
if(str1[i-1]==str2[j-1]){
lcs[idx]=str1[i-1];
backtrack(i-1,j-1,idx-1,dp,str1,str2);
}
else {
if(dp[i-1][j] == dp[i][j-1]){
backtrack(i-1,j,idx,dp,str1,str2);
backtrack(i,j-1,idx,dp,str1,str2);
}
else if (dp[i-1][j]>dp[i][j-1]){
backtrack(i-1,j,idx,dp,str1,str2);
}
else{
backtrack(i,j-1,idx,dp,str1,str2);
}
}
}
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(str1[i-1] == str2[j-1]){
dp[i][j]=dp[i-1][j-1]+1;
}
else{
dp[i][j]=(dp[i-1][j]>dp[i][j-1])?dp[i-1][j]:dp[i][j-1];
}
}
}
int lcs_len=dp[m][n];
backtrack(m,n,lcs_len-1,dp,str1,str2);
for(int i=0;i<count;i++){
printf("%s\n",results[i]);
free(results[i]);
}
}
int main(){
char str1[MAX],str2[MAX];
fgets(str1,MAX,stdin);
fgets(str2,MAX,stdin);
str1[strcspn(str1,"\n")]='\0';
str2[strcspn(str2,"\n")]='\0';
find(str1,str2,strlen(str1),strlen(str2));
return 0;
}
5.
Emily is managing a factory that operates on sequential machines represented by matrices. Each
machine outputs data in a format that matches the input dimensions of the next machine. To reduce
processing time, Emily wants to determine the minimum number of scalar operations required to
process the sequence.
CODE:
#include <stdio.h>
#include <limits.h>
int main() {
int n;
scanf("%d", &n);
int d[n];
matrixChainOrder(d, n);
return 0;
}
6.
Daniel has a standard 8x8 chessboard, and he wants to place 8 queens on the board in such a way
that no two queens threaten each other. In chess, a queen can move horizontally, vertically, and
diagonally. Therefore, no two queens should share the same row, column, or diagonal.
Daniel's goal is to find a valid arrangement of the queens on the chessboard where they do not
threaten each other. If such an arrangement exists, he wants to display the positions of the queens
on the board.
Rules:
Each queen should be placed on a different row and column.
No two queens should be on the same row, column, or diagonal.
Input format :
No console input.
Output format :
The output displays the arrangement of the queens on the chessboard as an 8x8 grid, where:
Each cell containing a queen is represented by the number '1'.
Each empty cell is represented by the number '0'.
CODE:
#include <iostream>
#include <vector>
const int N = 8;
int main()
{
vector<vector<int> > board(N, vector<int>(N, 0));
solveNQueens(board, 0);
return 0;
}
7.
Given two strings, text1, and text2, return the length of their longest common subsequence. If there
is no common subsequence, return 0.
A subsequence of a string is a new string generated from the original string with some characters
(can be none) deleted without changing the relative order of the remaining characters. For example,
"ace" is a subsequence of "abcde".
CODE:
#include<stdio.h>
#include<string.h>
int longestCommonSubsequence(char*text1,char*text2) {
int len1=strlen(text1);
int len2=strlen(text2);
int dp[len1+1][len2+1];
for(int i=0;i<=len1;i++){
for(int j=0;j<=len2;j++){
if(i==0 || j==0){
dp[i][j]=0;
} else if(text1[i-1]==text2[j-1]){
dp[i][j]=dp[i-1][j-1]+1;
}else{
dp[i][j]=(dp[i-1][j]>dp[i][j-1])?dp[i-1][j]:dp[i][j-1];
}
}
}
return dp[len1][len2];
}
int main(){
char text1[101],text2[101];
scanf("%s",text1);
scanf("%s",text2);
printf("%d\n",longestCommonSubsequence(text1,text2));
return 0;
}
8.
Problem Statement
You are given a standard 4x4 chessboard, and your task is to place 4 rooks on the board in such a way
that no two rooks threaten each other. In chess, a rook can move horizontally and vertically.
Therefore, no two rooks should share the same row or column.
Your goal is to find a valid arrangement of the rooks on the chessboard where they do not threaten
each other. If such an arrangement exists, display the positions of the rooks on the board. If no valid
arrangement exists, indicate that no solution can be found.
Rules:
Each rook should be placed on a different row and column.
No two rooks should be on the same row or column.
Input format :
No console input.
Output format :
Display the arrangement of the rooks on the chessboard as a 4x4 grid, where:
Each cell containing a rook is represented by the number '1'.
Each empty cell is represented by the number '0'.
CODE:
#include <iostream>
#include <vector>
Raj is working on a project that involves searching for a specific pattern in a given text. He has heard
about the Knuth-Morris-Pratt (KMP) algorithm, which is efficient for string matching, and he decided
to implement it.
Write a program that takes a text and a pattern as input, and then applies the KMP algorithm to find
and print all pattern occurrences within the text.
Input format :
The first line of input consists of the text string txt, representing the main string where the pattern
needs to be searched.
The second line of input consists of the pattern string pat, that Raj wants to find within the text as a
string.
Output format :
If the pattern is found, print "Found pattern at index i", where i is the index of the first occurrence of
the pattern in the text.
If the pattern is not found, print "Pattern not found".
CODE:
#define MAX_TEXT 50
#define MAX_PATTERN 25
int main() {
char txt[MAX_TEXT + 1];
char pat[MAX_PATTERN + 1];
scanf("%50s", txt);
scanf("%25s", pat);
KMPSearch(txt, pat);
return 0;
}
10.
Anil is working on a project that involves counting occurrences of a specific pattern within a given
text. He needs a program that efficiently finds and counts all occurrences of a pattern within a given
text using the Knuth-Morris-Pratt (KMP) algorithm.
Your task is to help Anil by developing a program that takes a text and a pattern as input and counts
the number of times the pattern appears in the text using the KMP algorithm.
Input format :
The first line contains a string txt, representing the text in which Anil wants to search for the pattern.
The second line contains a string pat, representing the pattern Anil wants to search for in the text.
Output format :
The output displays a single integer, representing the count of occurrences of the pattern pat in the
text txt.
If the pattern is not found, nothing will be printed.
#define MAX_TEXT 50
#define MAX_PATTERN 25
int main() {
char txt[MAX_TEXT + 1];
char pat[MAX_PATTERN + 1];
scanf("%50s", txt);
scanf("%25s", pat);
int result = KMPSearch(txt, pat);
if (result > 0) {
printf("%d\n", result);
}
return 0;
}