SlideShare a Scribd company logo
1 of 10
Find an LCS of X = and Y = Show the c and b table. Attach
File Let S = (a_1, a_2, a_3, ..., a_12) be a set with n = 12
activities. The start time and the finish time of each activity is
shown in the table below. Run the GREEDY-ACTIVITY-
SELECTOR function learned in class. What is the subset of
activities returned? Write the activities in the correct order in
which they are selected by the algorithm.
Solution
// C++ program to rearrange a string so that all same
// characters become at least d distance away
#include <iostream>
#include <cstring>
#include <cstdlib>
#define MAX 256
using namespace std;
// A structure to store a character 'c' and its frequency 'f'
// in input string
struct charFreq {
char c;
int f;
};
// A utility function to swap two charFreq items.
void swap(charFreq *x, charFreq *y) {
charFreq z = *x;
*x = *y;
*y = z;
}
// A utility function to maxheapify the node freq[i] of a heap
// stored in freq[]
void maxHeapify(charFreq freq[], int i, int heap_size)
{
int l = i*2 + 1;
int r = i*2 + 2;
int largest = i;
if (l < heap_size && freq[l].f > freq[i].f)
largest = l;
if (r < heap_size && freq[r].f > freq[largest].f)
largest = r;
if (largest != i)
{
swap(&freq[i], &freq[largest]);
maxHeapify(freq, largest, heap_size);
}
}
// A utility function to convert the array freq[] to a max heap
void buildHeap(charFreq freq[], int n)
{
int i = (n - 1)/2;
while (i >= 0)
{
maxHeapify(freq, i, n);
i--;
}
}
// A utility function to remove the max item or root from max
heap
charFreq extractMax(charFreq freq[], int heap_size)
{
charFreq root = freq[0];
if (heap_size > 1)
{
freq[0] = freq[heap_size-1];
maxHeapify(freq, 0, heap_size-1);
}
return root;
}
// The main function that rearranges input string 'str' such that
// two same characters become d distance away
void rearrange(char str[], int d)
{
// Find length of input string
int n = strlen(str);
// Create an array to store all characters and their
// frequencies in str[]
charFreq freq[MAX] = {{0, 0}};
int m = 0; // To store count of distinct characters in str[]
// Traverse the input string and store frequencies of all
// characters in freq[] array.
for (int i = 0; i < n; i++)
{
char x = str[i];
// If this character has occurred first time, increment m
if (freq[x].c == 0)
freq[x].c = x, m++;
(freq[x].f)++;
str[i] = '0'; // This change is used later
}
// Build a max heap of all characters
buildHeap(freq, MAX);
// Now one by one extract all distinct characters from max
heap
// and put them back in str[] with the d distance constraint
for (int i = 0; i < m; i++)
{
charFreq x = extractMax(freq, MAX-i);
// Find the first available position in str[]
int p = i;
while (str[p] != '0')
p++;
// Fill x.c at p, p+d, p+2d, .. p+(f-1)d
for (int k = 0; k < x.f; k++)
{
// If the index goes beyond size, then string cannot
// be rearranged.
if (p + d*k >= n)
{
cout << "Cannot be rearranged";
exit(0);
}
str[p + d*k] = x.c;
}
}
}
// Driver program to test above functions
int main()
{
char str[] = "aabbcc";
rearrange(str, 3);
cout << str;
}
// C++ program to rearrange a string so that all same
// characters become at least d distance away
#include <iostream>
#include <cstring>
#include <cstdlib>
#define MAX 256
using namespace std;
// A structure to store a character 'c' and its frequency 'f'
// in input string
struct charFreq {
char c;
int f;
};
// A utility function to swap two charFreq items.
void swap(charFreq *x, charFreq *y) {
charFreq z = *x;
*x = *y;
*y = z;
}
// A utility function to maxheapify the node freq[i] of a heap
// stored in freq[]
void maxHeapify(charFreq freq[], int i, int heap_size)
{
int l = i*2 + 1;
int r = i*2 + 2;
int largest = i;
if (l < heap_size && freq[l].f > freq[i].f)
largest = l;
if (r < heap_size && freq[r].f > freq[largest].f)
largest = r;
if (largest != i)
{
swap(&freq[i], &freq[largest]);
maxHeapify(freq, largest, heap_size);
}
}
// A utility function to convert the array freq[] to a max heap
void buildHeap(charFreq freq[], int n)
{
int i = (n - 1)/2;
while (i >= 0)
{
maxHeapify(freq, i, n);
i--;
}
}
// A utility function to remove the max item or root from max
heap
charFreq extractMax(charFreq freq[], int heap_size)
{
charFreq root = freq[0];
if (heap_size > 1)
{
freq[0] = freq[heap_size-1];
maxHeapify(freq, 0, heap_size-1);
}
return root;
}
// The main function that rearranges input string 'str' such that
// two same characters become d distance away
void rearrange(char str[], int d)
{
// Find length of input string
int n = strlen(str);
// Create an array to store all characters and their
// frequencies in str[]
charFreq freq[MAX] = {{0, 0}};
int m = 0; // To store count of distinct characters in str[]
// Traverse the input string and store frequencies of all
// characters in freq[] array.
for (int i = 0; i < n; i++)
{
char x = str[i];
// If this character has occurred first time, increment m
if (freq[x].c == 0)
freq[x].c = x, m++;
(freq[x].f)++;
str[i] = '0'; // This change is used later
}
// Build a max heap of all characters
buildHeap(freq, MAX);
// Now one by one extract all distinct characters from max
heap
// and put them back in str[] with the d distance constraint
for (int i = 0; i < m; i++)
{
charFreq x = extractMax(freq, MAX-i);
// Find the first available position in str[]
int p = i;
while (str[p] != '0')
p++;
// Fill x.c at p, p+d, p+2d, .. p+(f-1)d
for (int k = 0; k < x.f; k++)
{
// If the index goes beyond size, then string cannot
// be rearranged.
if (p + d*k >= n)
{
cout << "Cannot be rearranged";
exit(0);
}
str[p + d*k] = x.c;
}
}
}
// Driver program to test above functions
int main()
{
char str[] = "aabbcc";
rearrange(str, 3);
cout << str;
}

More Related Content

Similar to Find an LCS of X = and Y = Show the c and b table. Attach File .docx

So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdf
ezonesolutions
 
Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdfCan anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
arjunhassan8
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questions
Srikanth
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3
Srikanth
 
The purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdfThe purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdf
Rahul04August
 

Similar to Find an LCS of X = and Y = Show the c and b table. Attach File .docx (20)

Templates
TemplatesTemplates
Templates
 
C++11 - STL Additions
C++11 - STL AdditionsC++11 - STL Additions
C++11 - STL Additions
 
So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdf
 
Overloading
OverloadingOverloading
Overloading
 
Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdfCan anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Lecture5
Lecture5Lecture5
Lecture5
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questions
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3
 
Ds lec 14 filing in c++
Ds lec 14 filing in c++Ds lec 14 filing in c++
Ds lec 14 filing in c++
 
The purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdfThe purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdf
 
Unit 3 arrays and_string
Unit 3 arrays and_stringUnit 3 arrays and_string
Unit 3 arrays and_string
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Arrays
ArraysArrays
Arrays
 
Strings
StringsStrings
Strings
 
GeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetGeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheet
 
Functions
FunctionsFunctions
Functions
 
Pointer
PointerPointer
Pointer
 

More from Abdulrahman890100 (6)

game.c Written by Peter Sutton. Game board da.docx
  game.c   Written by Peter Sutton.   Game board da.docx  game.c   Written by Peter Sutton.   Game board da.docx
game.c Written by Peter Sutton. Game board da.docx
 
I am having issues writing the loop language that i am using is C .docx
  I am having issues writing the loop language that i am using is C .docx  I am having issues writing the loop language that i am using is C .docx
I am having issues writing the loop language that i am using is C .docx
 
I have two classes One is Carnivore and the other is Herbivore. .docx
  I have two classes One is Carnivore and the other is Herbivore. .docx  I have two classes One is Carnivore and the other is Herbivore. .docx
I have two classes One is Carnivore and the other is Herbivore. .docx
 
Complete the C++ program and implement the routines that are not .docx
  Complete the C++ program and implement the routines that are not .docx  Complete the C++ program and implement the routines that are not .docx
Complete the C++ program and implement the routines that are not .docx
 
Create an application that calculates the total cost of a hospit.docx
  Create an application that calculates the total cost of a hospit.docx  Create an application that calculates the total cost of a hospit.docx
Create an application that calculates the total cost of a hospit.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

Find an LCS of X = and Y = Show the c and b table. Attach File .docx

  • 1. Find an LCS of X = and Y = Show the c and b table. Attach File Let S = (a_1, a_2, a_3, ..., a_12) be a set with n = 12 activities. The start time and the finish time of each activity is shown in the table below. Run the GREEDY-ACTIVITY- SELECTOR function learned in class. What is the subset of activities returned? Write the activities in the correct order in which they are selected by the algorithm. Solution // C++ program to rearrange a string so that all same // characters become at least d distance away #include <iostream> #include <cstring> #include <cstdlib> #define MAX 256 using namespace std; // A structure to store a character 'c' and its frequency 'f' // in input string struct charFreq { char c; int f; }; // A utility function to swap two charFreq items.
  • 2. void swap(charFreq *x, charFreq *y) { charFreq z = *x; *x = *y; *y = z; } // A utility function to maxheapify the node freq[i] of a heap // stored in freq[] void maxHeapify(charFreq freq[], int i, int heap_size) { int l = i*2 + 1; int r = i*2 + 2; int largest = i; if (l < heap_size && freq[l].f > freq[i].f) largest = l; if (r < heap_size && freq[r].f > freq[largest].f) largest = r; if (largest != i) { swap(&freq[i], &freq[largest]); maxHeapify(freq, largest, heap_size); } } // A utility function to convert the array freq[] to a max heap void buildHeap(charFreq freq[], int n) {
  • 3. int i = (n - 1)/2; while (i >= 0) { maxHeapify(freq, i, n); i--; } } // A utility function to remove the max item or root from max heap charFreq extractMax(charFreq freq[], int heap_size) { charFreq root = freq[0]; if (heap_size > 1) { freq[0] = freq[heap_size-1]; maxHeapify(freq, 0, heap_size-1); } return root; } // The main function that rearranges input string 'str' such that // two same characters become d distance away void rearrange(char str[], int d) { // Find length of input string int n = strlen(str);
  • 4. // Create an array to store all characters and their // frequencies in str[] charFreq freq[MAX] = {{0, 0}}; int m = 0; // To store count of distinct characters in str[] // Traverse the input string and store frequencies of all // characters in freq[] array. for (int i = 0; i < n; i++) { char x = str[i]; // If this character has occurred first time, increment m if (freq[x].c == 0) freq[x].c = x, m++; (freq[x].f)++; str[i] = '0'; // This change is used later } // Build a max heap of all characters buildHeap(freq, MAX); // Now one by one extract all distinct characters from max heap // and put them back in str[] with the d distance constraint for (int i = 0; i < m; i++) { charFreq x = extractMax(freq, MAX-i); // Find the first available position in str[] int p = i;
  • 5. while (str[p] != '0') p++; // Fill x.c at p, p+d, p+2d, .. p+(f-1)d for (int k = 0; k < x.f; k++) { // If the index goes beyond size, then string cannot // be rearranged. if (p + d*k >= n) { cout << "Cannot be rearranged"; exit(0); } str[p + d*k] = x.c; } } } // Driver program to test above functions int main() { char str[] = "aabbcc"; rearrange(str, 3); cout << str; } // C++ program to rearrange a string so that all same // characters become at least d distance away
  • 6. #include <iostream> #include <cstring> #include <cstdlib> #define MAX 256 using namespace std; // A structure to store a character 'c' and its frequency 'f' // in input string struct charFreq { char c; int f; }; // A utility function to swap two charFreq items. void swap(charFreq *x, charFreq *y) { charFreq z = *x; *x = *y; *y = z; } // A utility function to maxheapify the node freq[i] of a heap // stored in freq[] void maxHeapify(charFreq freq[], int i, int heap_size) { int l = i*2 + 1; int r = i*2 + 2; int largest = i; if (l < heap_size && freq[l].f > freq[i].f)
  • 7. largest = l; if (r < heap_size && freq[r].f > freq[largest].f) largest = r; if (largest != i) { swap(&freq[i], &freq[largest]); maxHeapify(freq, largest, heap_size); } } // A utility function to convert the array freq[] to a max heap void buildHeap(charFreq freq[], int n) { int i = (n - 1)/2; while (i >= 0) { maxHeapify(freq, i, n); i--; } } // A utility function to remove the max item or root from max heap charFreq extractMax(charFreq freq[], int heap_size) { charFreq root = freq[0]; if (heap_size > 1)
  • 8. { freq[0] = freq[heap_size-1]; maxHeapify(freq, 0, heap_size-1); } return root; } // The main function that rearranges input string 'str' such that // two same characters become d distance away void rearrange(char str[], int d) { // Find length of input string int n = strlen(str); // Create an array to store all characters and their // frequencies in str[] charFreq freq[MAX] = {{0, 0}}; int m = 0; // To store count of distinct characters in str[] // Traverse the input string and store frequencies of all // characters in freq[] array. for (int i = 0; i < n; i++) { char x = str[i]; // If this character has occurred first time, increment m if (freq[x].c == 0) freq[x].c = x, m++; (freq[x].f)++;
  • 9. str[i] = '0'; // This change is used later } // Build a max heap of all characters buildHeap(freq, MAX); // Now one by one extract all distinct characters from max heap // and put them back in str[] with the d distance constraint for (int i = 0; i < m; i++) { charFreq x = extractMax(freq, MAX-i); // Find the first available position in str[] int p = i; while (str[p] != '0') p++; // Fill x.c at p, p+d, p+2d, .. p+(f-1)d for (int k = 0; k < x.f; k++) { // If the index goes beyond size, then string cannot // be rearranged. if (p + d*k >= n) { cout << "Cannot be rearranged"; exit(0); } str[p + d*k] = x.c;
  • 10. } } } // Driver program to test above functions int main() { char str[] = "aabbcc"; rearrange(str, 3); cout << str; }