arr1=[]
arr2=[]
def permu(arr,fi,n):
if fi==len(arr)-1:
temp=””
for i in arr:
temp=temp+i
if n==1:
for p in range(len(arr)):
for q in range(p,len(arr)):
qwerty=temp[p:q]
if qwerty not in arr1:
arr1.append(qwerty)
else:
for p in range(len(arr)):
for q in range(p,len(arr)):
qwerty=temp[p:q]
if qwerty not in arr2:
arr2.append(qwerty)
return
for i in range(fi,len(arr)):
arr[i],arr[fi]=arr[fi],arr[i]
permu(arr,fi+1,n)
arr[i],arr[fi]=arr[fi],arr[i]
x=”AGGTAB”
y=”GXTXAYB”
permu(list(x),0,1)
permu(list(y),0,2)
arr=[]
for i in arr1:
if i in arr2:
arr.append(i)
ans=arr[0]
for i in range(1,len(arr)):
if len(arr[i]) > len(ans):
ans=arr[i]
print(ans)
def lcs(a, b):
len_a = len(a)
len_b = len(b)
arr = [ [0 for j in range(len_b + 1)] for i in range(len_a + 1)]
#print(arr)
for i in range(len_a):
for j in range(len_b):
if i == 0 or j == 0:
arr[i][j] = 0
elif a[i-1] == b[j-1]:
arr[i][j] = arr[i-1][j-1] + 1
else:
arr[i][j] = max(arr[i-1][j], arr[i][j-1])
print(arr)
return arr[len_a-1][len_b-1] + 1
#include
#include
#include
using namespace std;
/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */
int findthat(string k,string a){
setc;
int count=0;
for(int i=0;i<k.length();i++){
c.insert(k[i]);
}
for(int i=0;i>k>>a;
cout<<findthat(k,a);
return 0;
}
arr1=[]
arr2=[]
def permu(arr,fi,n):
if fi==len(arr)-1:
temp=””
for i in arr:
temp=temp+i
if n==1:
for p in range(len(arr)):
for q in range(p,len(arr)):
qwerty=temp[p:q]
if qwerty not in arr1:
arr1.append(qwerty)
else:
for p in range(len(arr)):
for q in range(p,len(arr)):
qwerty=temp[p:q]
if qwerty not in arr2:
arr2.append(qwerty)
return
for i in range(fi,len(arr)):
arr[i],arr[fi]=arr[fi],arr[i]
permu(arr,fi+1,n)
arr[i],arr[fi]=arr[fi],arr[i]
x=”AGGTAB”
y=”GXTXAYB”
permu(list(x),0,1)
permu(list(y),0,2)
arr=[]
for i in arr1:
if i in arr2:
arr.append(i)
ans=arr[0]
for i in range(1,len(arr)):
if len(arr[i]) > len(ans):
ans=arr[i]
print(ans)
public static int lcsdp(String s,String t,int i,int j,int dp[][]) {
if(i==s.length())
return 0;
if(j==t.length())
return 0;
if(dp[i][j]!= -1) {
return dp[i][j];
}
int res;
if(s.charAt(i)==t.charAt(j))
res=1+lcsdp(s,t,i+1,j+1,dp);
else
res=Math.max(lcsdp(s,t,i+1,j,dp),lcsdp(s,t,i,j+1,dp));
return dp[i][j]=res;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String s=”abcde”;
String t=”ace”;
int dp[][]=new int[s.length()][t.length()];
for(int i=0;i<s.length();i++) {
for(int j=0;j<t.length();j++) {
dp[i][j]=-1;
}
}
System.out.println(lcsdp(s,t,0,0,dp));
}
def lcs(a,b):
c=len(a)
d=len(b)
print(“Max Length is: \n”,max(c,d))
a=input(“Enter your string: \n”)
b=input(“Enter your string 2: \n”)
lcs(a,b)
# Longest common subsequence
a, b = input().split()
def lcs(a, b):
len_a = len(a)
len_b = len(b)
arr = [ [0 for j in range(len_b + 1)] for i in range(len_a + 1)]
#print(arr)
for i in range(len_a):
for j in range(len_b):
if i == 0 or j == 0:
arr[i][j] = 0
elif a[i-1] == b[j-1]:
arr[i][j] = arr[i-1][j-1] + 1
else:
arr[i][j] = max(arr[i-1][j], arr[i][j-1])
print(arr)
return arr[len_a-1][len_b-1] + 1
print(lcs(a, b))
#include
#include
int main()
{
int count = 0,i,k;
char X[] = “AGGTAB”;
char Y[] = “GXTXAYB”;
int m = strlen(X);
int n = strlen(Y);
for(i=0;i<m;i++)
{
for(k=0;k<n;k++)
{
if(X[i]==Y[k])
{
count = count+1;
Y[k]= ' ';
break;
}
}
}
printf("%d",count);
return 0;
}
a=input()
b=input()
l=[]
for i in a:
c=0
for j in b:
if i==j:
c=1
if c==1:
l.append(i)
c=set(l)
print(“”.join(c))
print(“of lenth”,len(c))
#include
#include
#include
using namespace std;
/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */
int findthat(string k,string a){
setc;
int count=0;
for(int i=0;i<k.length();i++){
c.insert(k[i]);
}
for(int i=0;i>k>>a;
cout<<findthat(k,a);
return 0;
}
PYTHON CODE:
def lcs(X, Y, m, n):
if m == 0 or n == 0:
return 0;
elif X[m-1] == Y[n-1]:
return 1 + lcs(X, Y, m-1, n-1);
else:
return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));
X,Y=map(str,input().split(” and “))
print(“Length of LCS is “, lcs(X , Y, len(X), len(Y)))
Its showing error in #include
#include
void main()
{
char arr[6],brr[6],crr[6];
int i,j=0,k=1,m=0;
printf(“enter first string”);
gets(arr);
fflush(stdin);
printf(“enter second string”);
gets(arr);
printf(“%s\n”,arr);
for(i=0;i<6;i++)
{
if(j!=k)
{
for(j=0;j<6;j++)
if(arr[i]==brr[j])
{
crr[m]=arr[i];
m++;
k=j;
break;
}
}
else
{
for(j=k+1;j<6;j++)
{
if(arr[i]==brr[j])
{
crr[m]=arr[i];
m++;
k=j;
break;
}
}
}
}
printf("%d",c);
}