Can someone please help me? I can't figure out how to code this assignment I have. The assignment is that I have to look for overlaps.
Example of Overlap:
Keyword: face
Comparison words: fact war stare cent facet endface
# Overlap between face and fact: 0
# Overlap between face and war: 0
# Overlap between face and stare: 0
# Overlap between face and cent: 2 (ce)
# Overlap between face and facet: 4 (face)
# Overlap between face and endface: 4 (e and face, but we take the bigger overlap)
Do you get it? So an overlap happens if the end of the keyword overlaps with the beginning of the comparison word, or the beginning of the keyword with the end of the comparison word.
Also, I need to take into account the max overlap. So in this example, my keyword was face and my biggest overlap was 4 (but since there were two 4's, I take the input that occurred first).
Thus, if I output the results to the screen after going through every input, I should get:
Keyword: face
Match: facet
Overlap: 4
Here's what I have so far...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <iostream>
using namespace std;
int overlapFront(string& , string& );
int overlapEnd(string& , string& );
void overlapMax(int& , string& , int , string&);
int main()
{
string input;
int overlap = 0;
int overlap2 = 0;
int newMax = 0;
int oldMax = 0;
string oldInput = " ";
string key1, key2, key3;
cin >> key1; // testing only one keyword right now
while (cin >> input)
{
overlap = overlapFront(key1, input);
overlap2 = overlapEnd(key1, input);
if (overlap2 > overlap)
{
newMax = overlap2;
}
else if (overlap == 0 && overlap2 == 0)
{
newMax = 0;
}
else if (overlap > overlap2)
{
newMax = overlap;
}
overlapMax(oldMax, oldInput, newMax, input);
cout << "Key: " << key1 << endl;
cout << "Match: " << input << endl;
cout << "Overlap: " << overlap << " " << overlap2 << endl;
cout << "Max: " << newMax << endl << endl;
}
return 0;
}
int overlapFront(string& key1, string& input)
{
int overlap = 0;
string match1 = input;
string key = key1;
if (input.substr(0, key1.size()) == key1)
{
overlap = key1.size();
match1 = input;
}
return overlap;
}
int overlapEnd(string& key1, string& input)
{
int overlap = 0;
string match = input;
string key = key1;
if (input.at(input.size() - 1) == key1.at(0))
{
overlap = 1;
match = input;
}
return overlap;
}
void overlapMax(int& oldMax, string& oldInput, int newMax, const string& cnst)
{
int maxInt = oldMax;
string maxS = oldInput;
string newS = cnst;
if (newMax > maxInt)
{
oldMax = newMax;
oldInput = newS;
}
}
|
I have used 3 functions. One for getting the # of overlaps at the front, one at the back, and for the max overlap and its associated string (input). But I am not complete as I don't know how to test for cases like face and cent (overlap = 2), or cases like face and sofa (overlap = 2), etc.
Also, I'm doing my overlapMax function wrong because when I compile, there was a problem with it. Please help if you can.. I really need to get this done today. Thank you!!