Problem A: (A Type of) Sequence Matching (20 points) One string analysis task th
ID: 3801722 • Letter: P
Question
Problem A: (A Type of) Sequence Matching (20 points) One string analysis task that played a crucial role in mapping the human genome is the following: given two strings s 1 and s2, find the largest substring occurring at both the very beginning of s1 and the very end of s2. For example, if s1 is the string ACTTTCTAT and s2 is the string TG GTTTTG. CT, notice that ACT occurs at the beginning of s1 and the end of s2. Moreover, no longer substring occurring at the beginning of s 1 so occurs at the end of s 2: for example, the first four characters of s1, namely ACTT, are not the final four characters of s2; the first five characters of s1, namely ACTTT, are not the final five characters of s2; and so on. Write a C++ program that does the following: 1. Asks the user to input the two strings s 1 and s2 2. Finds the longest substring that occurs both at the beginning of s1 and the end of s2 3. Prints out that substring and its length. 4. Has a continuation loop, so the user can repeat the above steps as many times as he or she likes Here are additional comments and requirements: Do not make any assumptions about the strings' lengths. In particular, the two strings might have different lengths Your program should store the strings as C++-style strings. You may use any of the string class functions mentioned in the textbook Your solution should not be a long program, but think carefully about the program logic.Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
string s1;
string s2;
bool quit = false;
while(!quit){
cout << "Enter 2 string :" << endl;
cin >>s1;
cin >>s2;
int l1 = s1.length();
int l2 = s2.length();
int minLength = l1;
if(l1>l2)
minLength = l2;
int i=0;
int j=l2-1;
int len = 1;
string longestStr;
int longestLen = 0;
while(i<minLength){
string subs1 = s1.substr(0,len);
string subs2 = s2.substr(j,len);
if(subs1.compare(subs2)==0){
longestLen = len;
longestStr = subs1;
}
i++;
j--;
len++;
}
cout << "Longest Match : "<<longestStr<<endl;
cout << "Match Length : "<<longestLen<<endl<<endl;
cout << "Would you like to input 2 more strings(Y/N): ";
char ch;
cin >>ch;
if(ch=='y'||ch=='Y')
continue;
else
quit = true;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.