Skip to content

Commit

Permalink
String Merging #JC2018
Browse files Browse the repository at this point in the history
  • Loading branch information
harrypotter0 committed Jan 21, 2018
1 parent 602dd86 commit 9df1bac
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 38 deletions.
Empty file added CP/GST January SRM/problem3.cpp
Empty file.
19 changes: 0 additions & 19 deletions CP/January Challenge 2018/Untitled Document

This file was deleted.

Binary file added CP/January Challenge 2018/a.out
Binary file not shown.
85 changes: 85 additions & 0 deletions CP/January Challenge 2018/string.cpp
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;
}
63 changes: 49 additions & 14 deletions CP/January Challenge 2018/string.py
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)
5 changes: 0 additions & 5 deletions dynamic/longest_common_substring.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
Given two sequences A = [A1, A2, A3,..., An] and B = [B1, B2, B3,..., Bm], find the length of the longest common
substring.
Video
-----
* https://youtu.be/BysNXJHzCEs
Complexity
----------
Expand Down Expand Up @@ -77,4 +73,3 @@ def longest_common_substring(str1, str2):
str2 = "zcdemf"
expected = 3
assert expected == longest_common_substring(str1, str2)

0 comments on commit 9df1bac

Please sign in to comment.