Skip to content

Commit 9df1bac

Browse files
committed
String Merging #JC2018
1 parent 602dd86 commit 9df1bac

File tree

6 files changed

+134
-38
lines changed

6 files changed

+134
-38
lines changed

CP/GST January SRM/problem3.cpp

Whitespace-only changes.

CP/January Challenge 2018/Untitled Document

Lines changed: 0 additions & 19 deletions
This file was deleted.

CP/January Challenge 2018/a.out

14 KB
Binary file not shown.

CP/January Challenge 2018/string.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
typedef long long ll;
4+
/* Returns length of LCS for X[0..m-1], Y[0..n-1] */
5+
ll lcs( string X, string Y, ll m, ll n )
6+
{
7+
int L[m+1][n+1];
8+
int i, j;
9+
10+
/* Following steps build L[m+1][n+1] in bottom up fashion. Note
11+
that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] */
12+
for (i=0; i<=m; i++)
13+
{
14+
for (j=0; j<=n; j++)
15+
{
16+
if (i == 0 || j == 0)
17+
L[i][j] = 0;
18+
19+
else if (X[i-1] == Y[j-1])
20+
L[i][j] = L[i-1][j-1] + 1;
21+
22+
else
23+
L[i][j] = max(L[i-1][j], L[i][j-1]);
24+
}
25+
}
26+
27+
/* L[m][n] contains length of LCS for X[0..n-1] and Y[0..m-1] */
28+
return L[m][n];
29+
}
30+
31+
char * removeDuplicates(char S[]){
32+
33+
int n = strlen(S);
34+
35+
// We don't need to do anything for
36+
// empty or single character string.
37+
if (n < 2)
38+
return S;
39+
40+
// j is used to store index is result
41+
// string (or index of current distinct
42+
// character)
43+
int j = 0;
44+
45+
// Traversing string
46+
for (int i=1; i<n; i++)
47+
{
48+
// If current character S[i]
49+
// is different from S[j]
50+
if (S[j] != S[i])
51+
{
52+
j++;
53+
S[j] = S[i];
54+
}
55+
}
56+
57+
// Putting string termination
58+
// character.
59+
j++;
60+
S[j] = '\0';
61+
return S;
62+
}
63+
64+
int main()
65+
{
66+
ll t,n,m,i,x,y;
67+
cin >> t;
68+
while(t--)
69+
{
70+
cin >> n >> m;
71+
char a[n],b[m];
72+
string a1,b1;
73+
scanf("%s",a);
74+
scanf("%s",b);
75+
a1 = removeDuplicates(a); //cout<<removeDuplicates(a)<<endl;
76+
b1 = removeDuplicates(b); //cout<<removeDuplicates(b)<<endl;
77+
x = a1.length();
78+
y = b1.length();
79+
//cout << x << " "<< y;
80+
ll l = lcs(a1,b1,x,y);
81+
//cout<< l;
82+
printf("%lld\n",(x+y-l));
83+
}
84+
return 0;
85+
}

CP/January Challenge 2018/string.py

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,54 @@
1-
def longest_common_subsequence(sequence1, sequence2):
2-
cols = len(sequence1) + 1 # Add 1 to represent 0 valued column for DP
3-
rows = len(sequence2) + 1 # Add 1 to represent 0 valued row for DP
1+
def lcs(X , Y):
2+
# find the length of the strings
3+
m = len(X)
4+
n = len(Y)
45

5-
T = [[0 for _ in range(cols)] for _ in range(rows)]
6+
# declaring the array for storing the dp values
7+
L = [[None]*(n+1) for i in range(m+1)]
68

7-
max_length = 0
8-
9-
for i in range(1, rows):
10-
for j in range(1, cols):
11-
if sequence2[i - 1] == sequence1[j - 1]:
12-
T[i][j] = 1 + T[i - 1][j - 1]
9+
"""Following steps build L[m+1][n+1] in bottom up fashion
10+
Note: L[i][j] contains length of LCS of X[0..i-1]
11+
and Y[0..j-1]"""
12+
for i in range(m+1):
13+
for j in range(n+1):
14+
if i == 0 or j == 0 :
15+
L[i][j] = 0
16+
elif X[i-1] == Y[j-1]:
17+
L[i][j] = L[i-1][j-1]+1
1318
else:
14-
T[i][j] = max(T[i - 1][j], T[i][j - 1])
15-
16-
max_length = max(max_length, T[i][j])
19+
L[i][j] = max(L[i-1][j] , L[i][j-1])
1720

18-
return max_length
21+
# L[m][n] contains the length of LCS of X[0..n-1] & Y[0..m-1]
22+
return L[m][n]
1923

24+
for _ in range(int(input())):
25+
n,m = input().split()
26+
n,m = int(n),int(m)
27+
a = input()
28+
b = input()
29+
i=1
30+
pre=a[0]
31+
c=""
32+
c+=pre
33+
while i<n:
34+
if a[i]!=pre:
35+
pre = a[i]
36+
c += pre
37+
i+=1
38+
a=c
39+
i=1
40+
pre=b[0]
41+
c=""
42+
c=pre
43+
while i<m:
44+
if b[i]!=pre:
45+
pre = b[i]
46+
c += pre
47+
i+=1
48+
b=c
49+
n=len(a)
50+
m=len(b)
51+
lc = lcs(a,b)
52+
f1 = n-lc
53+
f1+= (m-lc)+lc
54+
print(f1)

dynamic/longest_common_substring.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
Given two sequences A = [A1, A2, A3,..., An] and B = [B1, B2, B3,..., Bm], find the length of the longest common
66
substring.
77
8-
Video
9-
-----
10-
11-
* https://youtu.be/BysNXJHzCEs
128
139
Complexity
1410
----------
@@ -77,4 +73,3 @@ def longest_common_substring(str1, str2):
7773
str2 = "zcdemf"
7874
expected = 3
7975
assert expected == longest_common_substring(str1, str2)
80-

0 commit comments

Comments
 (0)