Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.
#include <iostream>
using namespace std;
void makeTable(string, string);
void print(int tableVals[][], int out, int in);
int main (int argc, const char * argv[])
{
string firstString;
string secondString;
cout << "Enter the first string: ";
cin >> firstString;
cout << "Enter the second string: ";
cin >> secondString;
cout << "You have entered " << firstString << " as your first string, and " << secondString << " as your second string." <<endl;
cout << firstString << " is " << firstString.length() << " long." <<endl;
cout << secondString << " is " << secondString.length() << " long." <<endl;
if(firstString.length() < secondString.length())
makeTable(firstString, secondString);
else
makeTable(secondString, firstString);
return 0;
}
void makeTable(string s1, string s2){
int tableVals[s1.length()][s2.length()];
int rowSum[s1.length()];
int colSum[s2.length()];
int tempx = 0;
int tempy = 0;
int temp = 0;
int min = 0;
int max = 0;
int startIndex = 0;
int endIndex = 0;
//Initialize rowSum
for(int x = 0; x < s1.length(); x++){
rowSum[x] = 0;
}
//Initialize colSum
for(int x = 0; x < s2.length(); x++){
colSum[x] = 0;
}
//Build the table
for(int i=0; i<s1.size(); i++){
for(int j=0; j<s2.size(); j++){
if(s1[i] == s2[j])
tableVals[i][j] = 1;
else
tableVals[i][j] = 0;
}
}
//Print the table
for(int x = 0; x < s1.length() ; x++){
for(int y = 0; y < s2.length() ; y++){
cout << tableVals[x][y];
}
cout << endl;
}
//Calculate the rowSum
for(int x = 0; x < s1.length(); x++){
for(int j = 0; j < s2.length(); j++){
rowSum[x] += tableVals[x][j];
}
}
//print the rowSum
cout << "The values in rowSum are: \n";
for(int x = 0; x < s1.size(); x++){
cout << rowSum[x];
}
//Calculate the colSum
for(int x = 0; x < s2.length(); x++){
for(int j = 0; j < s1.length(); j++){
colSum[x] += tableVals[j][x];
}
}
//print the colSum
cout << "\nThe values in colSum is: \n";
for(int x = 0; x < s2.size(); x++){
cout << colSum[x];
}
//Get the start and end indexes
for(int x = 0; x < s2.length(); x++){
temp++;
min++;
if((colSum[x] >= 1)&&(colSum[x-1] == 0) && (temp > max) && (rowSum[x] > 0)){
startIndex = x;
}
else if((rowSum[x] > 0) && (colSum[x] == 0) && (min > max)){
max = min;
startIndex = x-max;
if(startIndex < 0){
cout << max;
}
endIndex = x;
temp = 0;
min = 0;
}
}
cout << "\nstartIndex = " << startIndex <<endl;
cout << "endIndex = " << endIndex <<endl;
//print out the longest substring
cout << "\nThe longest substring between " << s1 << " and " << s2 << " is: ";
for(int x = startIndex; x < s2.length(); x++){
if(colSum[x] >= 1)
cout << s2[x];
else
cout << " ";
}
cout << endl;
}
Enter the first string: thisworks
Enter the second string: hellothisworks
You have entered thisworks as your first string, and hellothisworks as your second string.
thisworks is 9 long.
hellothisworks is 14 long.
00000100000000
10000010000000
00000001000000
00000000100001
00000000010000
00001000001000
00000000000100
00000000000010
00000000100001
The values in rowSum are:
121212112
The values in colSum is:
100011112111122
startIndex = 4
endIndex = 1
The longest substring between thisworks and hellothisworks is: othisworks
for(int x = 0; x < s2.length(); x++){
temp++;
min++;
if((colSum[x] >= 1)&&(colSum[x-1] == 0) && (temp > max) && (rowSum[x] > 0)){
startIndex = x;
}
else if((rowSum[x] > 0) && (colSum[x] == 0) && (min > max)){
max = min;
startIndex = x-max;
if(startIndex < 0){
cout << max;
}
endIndex = x;
temp = 0;
min = 0;
}
}
for(int x = 0; x < s1.size(); x++){
for(int y = 0; y < s2.size(); y++){
if(tableVals[x][y] == 1){
tempx = x-1;
tempy = y-1;
if((tableVals[tempx][tempy] == 1) || (tableVals[tempx+2][tempy+2] == 1))
tableVals[x][y] = 1;
else
tableVals[x][y] = 0;
}
}
}
#include <iostream>
using namespace std;
void makeTable(string, string);
void print(int tableVals[][], int out, int in);
int main (int argc, const char * argv[])
{
string firstString;
string secondString;
cout << "Enter the first string: ";
cin >> firstString;
cout << "Enter the second string: ";
cin >> secondString;
cout << "You have entered " << firstString << " as your first string, and " << secondString << " as your second string." <<endl;
cout << firstString << " is " << firstString.length() << " long." <<endl;
cout << secondString << " is " << secondString.length() << " long." <<endl;
if(firstString.length() < secondString.length())
makeTable(firstString, secondString);
else
makeTable(secondString, firstString);
return 0;
}
void makeTable(string s1, string s2){
int tableVals[s1.length()][s2.length()];
int rowSum[s1.length()];
int colSum[s2.length()];
int tempx = 0;
int tempy = 0;
int pasty = 0;
//Initialize rowSum
for(int x = 0; x < s1.length(); x++){
rowSum[x] = 0;
}
//Initialize colSum
for(int x = 0; x < s2.length(); x++){
colSum[x] = 0;
}
//Build the table
for(int i=0; i<s1.size(); i++){
for(int j=0; j<s2.size(); j++){
if(s1[i] == s2[j])
tableVals[i][j] = 1;
else
tableVals[i][j] = 0;
}
}
//Print the table
for(int x = 0; x < s1.length() ; x++){
for(int y = 0; y < s2.length() ; y++){
cout << tableVals[x][y];
}
cout << endl;
}
//Calculate the rowSum
for(int x = 0; x < s1.length(); x++){
for(int j = 0; j < s2.length(); j++){
rowSum[x] += tableVals[x][j];
}
}
//print the rowSum
cout << "The values in rowSum are: \n";
for(int x = 0; x < s1.size(); x++){
cout << rowSum[x];
}
for(int x = 0; x < s1.size(); x++){
for(int y = 0; y < s2.size(); y++){
if(tableVals[x][y] == 1){
tempx = x-1;
tempy = y-1;
if((tableVals[x-1][y-1] == 1) || (tableVals[x+1][y+1] == 1))
tableVals[x][y] = 1;
else
tableVals[x][y] = 0;
}
}
}
//Print the table
cout << "\nThe new table is: " <<endl;
for(int x = 0; x < s1.length() ; x++){
for(int y = 0; y < s2.length() ; y++){
cout << tableVals[x][y];
}
cout << endl;
}
//Calculate the colSum
for(int x = 0; x < s2.length(); x++){
for(int j = 0; j < s1.length(); j++){
colSum[x] += tableVals[j][x];
}
}
//print the colSum
cout << "\nThe values in colSum is: \n";
for(int x = 0; x < s2.size(); x++){
cout << colSum[x];
}
//print out the longest substring
cout << "\nThe longest substring between " << s1 << " and " << s2 << " is: ";
for(int x = 0; x < s1.length(); x++){
for(int y = 0; y < s2.length(); y++){
if((tableVals[x][y] == 1) && ((tableVals[x+1][y+1] == 1) || tableVals[x-1][y-1] == 1)){
cout << s2[y];
pasty = y;
}
}
if(tableVals[x+1][pasty+1] == 0){
cout << endl;
}
}
}
Enter the first string: thisworks
Enter the second string: hellothisworks
You have entered thisworks as your first string, and hellothisworks as your second string.
thisworks is 9 long.
hellothisworks is 14 long.
00000100000000
10000010000000
00000001000000
00000000100001
00000000010000
00001000001000
00000000000100
00000000000010
00000000100001
The values in rowSum are:
121212112
The new table is:
00000100000000
10000010000000
00000001000000
00000000100000
00000000010000
00000000001000
00000000000100
00000000000010
00000000100001
The values in colSum is:
10000111211111
The longest substring between thisworks and hellothisworks is: thhisworks
for(int x = 0; x < s1.size(); x++){
for(int y = 0; y < s2.size(); y++){
if(tableVals[x][y] == 1){
tempx = x-1;
tempy = y-1;
if((tableVals[x-1][y-1] == 1) || (tableVals[x+1][y+1] == 1))
tableVals[x][y] = 1;
else
tableVals[x][y] = 0;
}
}
}
void makeTable(string s1, string s2){
vector< vector<int> > tableVals( s1.size(), vector<int>(s2.size()) );
>> it was driving me crazyvoid makeTable(string s1, string s2){
int tableVals[s1.length()+1][s2.length()+1];
int longest = 0;
string longestCS = "";
int tempx = 0;
int tempy = 0;
int pasty = 0;
for(int i = 0; i<s1.size()+1; i++){
for(int j = 0; j<s2.size()+1; j++){
tableVals[i][j]=0;
}
}
//Build the table
for(int i=0; i<s1.size()+1; i++){
for(int j=0; j<s2.size()+1; j++){
if(s1[i] == s2[j]){
tableVals[i+1][j+1] = tableVals[i][j] + 1;
longestCS = longestCS + s2[j];
longest++;
}
}
}
//Print the table
for(int x = 0; x < s1.length() ; x++){
for(int y = 0; y < s2.length() ; y++){
cout << tableVals[x][y];
}
cout << endl;
}
cout << "The longest common string is: " << longestCS <<endl;
}
#include <iostream>
using namespace std;
void makeTable(string, string);
int main (int argc, const char * argv[])
{
string firstString;
string secondString;
//Accept user input
cout << "Enter the first string: ";
cin >> firstString;
cout << "Enter the second string: ";
cin >> secondString;
//Display user input
cout << "You have entered " << firstString << " as your first string, and " << secondString << " as your second string." <<endl;
//Display string lengths
cout << firstString << " is " << firstString.length() << " long." <<endl;
cout << secondString << " is " << secondString.length() << " long." <<endl;
//Make sure that the longest string is passed as parameter #2 to the function.
if(firstString.length() < secondString.length())
makeTable(firstString, secondString);
else
makeTable(secondString, firstString);
return 0;
}
//This is a fairly straightforward function that simply makes a table out of the two substrings to discover
//the longest possible substring.
void makeTable(string s1, string s2){
int tableVals[s1.length()+1][s2.length()+1];
int longest = 0;
int secondLongest = 0;
int startX = 0;
int startY = 0;
string longestCS = "";
string properString = "";
//Initialize the table with 0's everywhere.
for(int i = 0; i<s1.size()+1; i++){
for(int j = 0; j<s2.size()+1; j++){
tableVals[i][j]=0;
}
}
//Build the table
for(int i=0; i<s1.size()+1; i++){
for(int j=0; j<s2.size()+1; j++){
if(s1[i] == s2[j]){
tableVals[i+1][j+1] = tableVals[i][j] + 1;
if(tableVals[i][j] == longest){
longest++;
startX = i;
startY = j;
}
if(tableVals[i][j] == secondLongest){
secondLongest++;
}
if(secondLongest > longest){
longest = secondLongest;
}
}
}
}
//Go through the string and print out the relevant substring.
for(int x = (startY-longest)+1; x <= startY; x++){
properString = properString + s2[x];
}
//Print the table
cout << "This is the table if you would like to examine the algorithm." <<endl;
for(int x = 0; x < s1.length() ; x++){
for(int y = 0; y < s2.length() ; y++){
cout << tableVals[x][y];
}
cout << endl;
}
cout << "The longest common substring is: " << properString <<endl;
}
I have therefore deleted three posts by theKashyap which were too precise in terms of a solution.My apologies. I was under the impression that we are expected to provide as precise a solution as possible.
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.