Question
C++ DIRECTIONS: analyze the code given to you through the debugger and find the problem. Analyze, do not need to find a solution #include #include
C++ DIRECTIONS: analyze the code given to you through the debugger and find the problem. Analyze, do not need to find a solution
#include
#include
#include
#include
using namespace std;
void getSumWindow(char * src, int sumWindow[], int len);
void getSumWindowAndSearch(char * pBig, char * pSmall, int lenBig, int lenSmall,
int sumWindowSmall[], int sumWindowBig[]);
/**************
*init and driver
*/
void align()
{
char str1[] = "Yellow fox jumps over the lazy dog";
char str2[] = "jumps over"; //smaller string should be found within the larger one
int len1 = strlen(str1);
int len2 = strlen(str2);
int sumWindowSmall[100] = { 0 }, sumWindowBig[100] = { 0 };
int lenSmall, lenBig;
char * pSmall, *pBig;
if (len1 < len2)
{
pSmall = str1;
pBig = str2;
lenSmall = len1;
lenBig = len2;
}
else
{
pSmall = str2;
pBig = str1;
lenSmall = len2;
lenBig = len1;
}
getSumWindow(pSmall, sumWindowSmall, lenSmall);
std::sort(sumWindowSmall, sumWindowSmall + lenSmall); //this is the only part with nlogn complexity,
//we sort smaller data set to improve in the speed
getSumWindowAndSearch(pBig, pSmall, lenBig, lenSmall, sumWindowSmall, sumWindowBig);
}
/****************
* For each position in the string, compute a sum of next ten characters (including the first one)
* And put that number into corresponding position of the int array.
* Note, that we only need to fully compute first ten chars. After that we slide the window
* by subtracting the previous number and adding the emerging.
*/
void getSumWindow(char * src, int sumWindow[], int len)
{
if (len < 10)
return;
for (int i = 0; i < 10; i++)
{
sumWindow[0] += src[i];
}
for (int i = 1; i < len - 10; i++)
{
sumWindow[i] = sumWindow[i - 1] - src[i - 1] + src[i + 9];
}
}
/***************************
* Similarly to the above, but while computing the sum on the fly, we immediately search
* for that number in the sorted array from the smaller string. When first match found, that
* means we found the overlap; and we are done. We only need to continue if no match has been found.
*/
void getSumWindowAndSearch(char * pBig, char * pSmall, int lenBig, int lenSmall,
int sumWindowSmall[], int sumWindowBig[])
{
if (lenBig < 10)
return;
for (int i = 0; i < 10; i++)
{
sumWindowBig[0] += pBig[i];
}
if (std::binary_search(sumWindowSmall, sumWindowSmall + lenSmall, sumWindowBig[0]))
{
pSmall[10] = '\0';
std::cout << " Two strings overlap with the string: " << pSmall;
return;
}
for (int i = 1; i < lenBig - 10; i++)
{
sumWindowBig[i] = sumWindowBig[i - 1] - pBig[i - 1] + pBig[i + 9];
if (std::binary_search(sumWindowSmall, sumWindowSmall + lenSmall, sumWindowBig[i]))
{
pSmall = pBig + i;
pSmall[i + 10] = '\0';
std::cout << " Two strings overlap with the string: " << pSmall;
return;
}
}
std::cout << " No overlap found";
return;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started