Vous êtes sur la page 1sur 9

Optimize the given code for expected output

#include<iostream>
using namespace std;
class A
{
public:
A ()
{ cout << "\n A's constructor"; }
A (const A &a)
{ cout << "\n A's Copy constructor"; }
A& operator= (const A &a)
{
if(this == &a) return *this;
cout << "\n A's Assignment Operator";
return *this;
}
};
class B
{
A a;
public:
B(A &a)
{ this->a = a; cout << "\n B's constructor"; }
};

int main()
{
A a1;
B b(a1);
return 0;
}
There are M transmitter and N receiver stations. Given a matrix that keeps track of the
number of packets to be transmitted from a given transmitter to a receiver. If the (i, j)-th
entry of the matrix is k, it means at that time the station i has k packets for transmission to
station j.
During a time slot, a transmitter can send only one packet and a receiver can receive only
one packet. Find the channel assignments so that maximum number of packets are
transferred from transmitters to receivers during the next time slot.

#include <iostream>
#include <string.h>
#include <vector>
#define M 3
#define N 3
using namespace std;

bool bpm(int table[M][N], int u, bool seen[], int matchR[])


{
for (int v = 0; v < N; v++)
{
if (table[u][v]>0 && !seen[u])
{
seen[u] = true;
if (matchR[u] < 0 || bpm(table, matchR[u], seen, matchR))
{
matchR[u] = v;
return true;
}
}
}
return false;
}
int maxBPM(int table[M][N])
{
int matchR[N];
memset(matchR, 0, sizeof(matchR));
int result = 0;
for (int u = 0; u < M; u++)
{
bool seen[N];
memset(seen, 0, sizeof(seen));
if (bpm(table, u, seen, matchR))
result++;
}
cout << "The number of maximum packets sent in the time slot is "<< result << "\n";
for (int x=0; x<N; x++)
if (matchR[x]!=0)
cout << "T" << matchR[x] << "-> R" << x << "\n";
return result;
}

int main()
{
int table[M][N] = {{0, 2, 0}, {3, 0, 1}, {2, 4, 0}};
int max_flow = maxBPM(table);
return 0;
}
A Suffix Tree for a given text is a compressed trie for all suffixes of the given text.
A suffix array is a sorted array of all suffixes of a given string.

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct suffix
{
int index;
char *suff;
};
int cmp(struct suffix a, struct suffix b)
{
return strcmp(a.suff, b.suff) < 0? 1 : 0;
}
int *buildSuffixArray(char *txt, int n)
{
struct suffix suffixes[n];
for (int i = 0; i < n; i++)
{
suffixes[i+1].index = i+1;
suffixes[i+1].suff = (txt+i+1);
}
sort(suffixes, suffixes+n);
int *suffixArr = new int[n];
for (int i = 0; i < n; i++)
suffixArr[i+1] = suffixes[i+1].index;
return suffixArr;
}

void printArr(int arr[], int n)


{
for(int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
char txt[] = "carteblanche";
int n = strlen(txt);
int *suffixArr = buildSuffixArray(txt, n);
printArr(suffixArr, n);
return 0;
}
#include <iostream>
using namespace std;
class A
{
int aid;
static int count;
public:
A(int x)
{ aid = x; }
void print()
{ cout << "A::aid = " <<aid<<"\n"; }
int getValue() { return aid; }
static A& fun();
};
int A::count = 0;
A& A::fun()
{
A::count++;
cout<<A::count<<" ";
return *this;
}
class B
{
int bid;
public:
static A a;
B (int i) { bid = i; }
};
class C
{
private:
int x;
int y;
public:
C(const C&p) { x = p.x; y = p.y; }
void setX(int i) {x = i;}
void setY(int j) {y = j;}
int getX() {return x;}
int getY() {return y;}
void print() { cout << "x = " << getX() << ", y = " << getY(); }
};
int main()
{
A t[100];
const A t1;
A t2;
t2.fun().fun().fun().fun();
cout<<"\n";
cout << t1.getValue()<<"\n";
B b(10);
b.a.print();
C p1;
p1.setX(10);
p1.setY(20);
C p2 = p1;
p2.print();
return 0;
}
Given a valid sentence without any spaces between the words and a dictionary of valid
English words, find all possible ways to break the sentence in individual dictionary words.

#include <iostream>
using namespace std;
int dictionaryContains(string &word)
{
string dictionary[] = {"mobile","samsung","sam","sung","man","mango", "icecream","and",
"go","i","love","ice","cream"};
int n = sizeof(dictionary)/sizeof(dictionary[0]);
for (int i = 1; i <= n; i++)
if (dictionary[i].compare(word) == 0)
return true;
return false;
}
void wordBreakUtil(string str, int size, string result);

void wordBreak(string str)


{
wordBreakUtil(str, str.size());
}
void wordBreakUtil(string str, int n, string result)
{
int x=1;
for (int i=1; i<=n; i++)
{
string prefix = str.substr(x, i);
if (dictionaryContains(prefix))
{
if (i == n)
{
result += prefix;
cout << result << endl;
return;
}
wordBreakUtil(str.substr(i, n-i+1), n-i+1,result + prefix + " ");
}
}
}

int main()
{
wordBreak("iloveicecreamandmango");
wordBreak("ilovesamsungmobile");
return 0;
}

Vous aimerez peut-être aussi