forked from harrypotter0/algorithms-in-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
602dd86
commit 9df1bac
Showing
6 changed files
with
134 additions
and
38 deletions.
There are no files selected for viewing
Empty file.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
/* Returns length of LCS for X[0..m-1], Y[0..n-1] */ | ||
ll lcs( string X, string Y, ll m, ll n ) | ||
{ | ||
int L[m+1][n+1]; | ||
int i, j; | ||
|
||
/* Following steps build L[m+1][n+1] in bottom up fashion. Note | ||
that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] */ | ||
for (i=0; i<=m; i++) | ||
{ | ||
for (j=0; j<=n; j++) | ||
{ | ||
if (i == 0 || j == 0) | ||
L[i][j] = 0; | ||
|
||
else if (X[i-1] == Y[j-1]) | ||
L[i][j] = L[i-1][j-1] + 1; | ||
|
||
else | ||
L[i][j] = max(L[i-1][j], L[i][j-1]); | ||
} | ||
} | ||
|
||
/* L[m][n] contains length of LCS for X[0..n-1] and Y[0..m-1] */ | ||
return L[m][n]; | ||
} | ||
|
||
char * removeDuplicates(char S[]){ | ||
|
||
int n = strlen(S); | ||
|
||
// We don't need to do anything for | ||
// empty or single character string. | ||
if (n < 2) | ||
return S; | ||
|
||
// j is used to store index is result | ||
// string (or index of current distinct | ||
// character) | ||
int j = 0; | ||
|
||
// Traversing string | ||
for (int i=1; i<n; i++) | ||
{ | ||
// If current character S[i] | ||
// is different from S[j] | ||
if (S[j] != S[i]) | ||
{ | ||
j++; | ||
S[j] = S[i]; | ||
} | ||
} | ||
|
||
// Putting string termination | ||
// character. | ||
j++; | ||
S[j] = '\0'; | ||
return S; | ||
} | ||
|
||
int main() | ||
{ | ||
ll t,n,m,i,x,y; | ||
cin >> t; | ||
while(t--) | ||
{ | ||
cin >> n >> m; | ||
char a[n],b[m]; | ||
string a1,b1; | ||
scanf("%s",a); | ||
scanf("%s",b); | ||
a1 = removeDuplicates(a); //cout<<removeDuplicates(a)<<endl; | ||
b1 = removeDuplicates(b); //cout<<removeDuplicates(b)<<endl; | ||
x = a1.length(); | ||
y = b1.length(); | ||
//cout << x << " "<< y; | ||
ll l = lcs(a1,b1,x,y); | ||
//cout<< l; | ||
printf("%lld\n",(x+y-l)); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,54 @@ | ||
def longest_common_subsequence(sequence1, sequence2): | ||
cols = len(sequence1) + 1 # Add 1 to represent 0 valued column for DP | ||
rows = len(sequence2) + 1 # Add 1 to represent 0 valued row for DP | ||
def lcs(X , Y): | ||
# find the length of the strings | ||
m = len(X) | ||
n = len(Y) | ||
|
||
T = [[0 for _ in range(cols)] for _ in range(rows)] | ||
# declaring the array for storing the dp values | ||
L = [[None]*(n+1) for i in range(m+1)] | ||
|
||
max_length = 0 | ||
|
||
for i in range(1, rows): | ||
for j in range(1, cols): | ||
if sequence2[i - 1] == sequence1[j - 1]: | ||
T[i][j] = 1 + T[i - 1][j - 1] | ||
"""Following steps build L[m+1][n+1] in bottom up fashion | ||
Note: L[i][j] contains length of LCS of X[0..i-1] | ||
and Y[0..j-1]""" | ||
for i in range(m+1): | ||
for j in range(n+1): | ||
if i == 0 or j == 0 : | ||
L[i][j] = 0 | ||
elif X[i-1] == Y[j-1]: | ||
L[i][j] = L[i-1][j-1]+1 | ||
else: | ||
T[i][j] = max(T[i - 1][j], T[i][j - 1]) | ||
|
||
max_length = max(max_length, T[i][j]) | ||
L[i][j] = max(L[i-1][j] , L[i][j-1]) | ||
|
||
return max_length | ||
# L[m][n] contains the length of LCS of X[0..n-1] & Y[0..m-1] | ||
return L[m][n] | ||
|
||
for _ in range(int(input())): | ||
n,m = input().split() | ||
n,m = int(n),int(m) | ||
a = input() | ||
b = input() | ||
i=1 | ||
pre=a[0] | ||
c="" | ||
c+=pre | ||
while i<n: | ||
if a[i]!=pre: | ||
pre = a[i] | ||
c += pre | ||
i+=1 | ||
a=c | ||
i=1 | ||
pre=b[0] | ||
c="" | ||
c=pre | ||
while i<m: | ||
if b[i]!=pre: | ||
pre = b[i] | ||
c += pre | ||
i+=1 | ||
b=c | ||
n=len(a) | ||
m=len(b) | ||
lc = lcs(a,b) | ||
f1 = n-lc | ||
f1+= (m-lc)+lc | ||
print(f1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters