12.3 P3 -Ex1: Substrings Compute and output the number of ways in which we can c
ID: 3740332 • Letter: 1
Question
12.3 P3 -Ex1: Substrings Compute and output the number of ways in which we can choose two identical non-overlapping substrings of a given string read from the user Constraints Read the string from the user . The given string will consi st only of lowercase English letters (a-z) The length of the giyen string will be between 1 and 50, inclusive * Output your result using the following format'%dn Example 1 When the input is Output must be Example 2 When the input is aba Output must be Example 3 When the input s abab Output must be 3 LAB ACTIVITY 123.1: P3- Ext: Substrings 0/ 65Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
void f(string str, int i, int j, int len,int& mx_len){
mx_len = max(mx_len,len);
if(j>=str.length())
return;
if(str[i]==str[j] && (j-i) > len)
f(str,i+1,j+1,len+1,mx_len);
f(str,i+1,j+1,0,mx_len);
f(str,i,j+1,0,mx_len);
}
int main(){
string s;
cout<<"Enter the string"<<endl;
cin>>s;
int mx_len=0;
f(s,0,1,0,mx_len);
cout<<mx_len<<endl;
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.