File tree 1 file changed +103
-0
lines changed
1 file changed +103
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*input
2
+ 1
3
+ 4 4
4
+ abab
5
+ baba
6
+ */
7
+
8
+
9
+ /*
10
+ Merging two strings into one string such that :
11
+ 1. For each string, their character occurs in the same relative order in Merged string as it occurs in the base strings,
12
+ 2. The minimum number of blocks consisting of consecutive identical characters is minimum in Merged string.
13
+ */
14
+
15
+
16
+ #include<bits/stdc++.h>
17
+
18
+ using namespace std;
19
+
20
+ typedef unsigned long long int ll;
21
+ typedef long int ln;
22
+ #define s(x) scanf("%d", &x)
23
+ #define loop(i, n) for(i =0 ; i<n ;i++)
24
+
25
+
26
+ int fun(char s[], char str[],int n, int m)
27
+ {
28
+ int a[m+1][n+1];
29
+ for(int i =0; i<=n ;i ++)
30
+ {
31
+ a[0][i]= i;
32
+ }
33
+
34
+ for(int i =1; i<=m ;i ++)
35
+ {
36
+ a[i][0]= i;
37
+ }
38
+
39
+ for(int i =1; i<= m ; i++)
40
+ {
41
+ for(int j =1 ; j<= n; j++)
42
+ {
43
+ if( str [i-1] == s[j-1] )
44
+ a[i][j] = min(a[i][j-1], min(a[i-1][j], a[i-1][j-1])) +1;
45
+ if( str [i-1] != s[j-1] )
46
+ a[i][j]= min (a[i][j-1], a[i-1][j])+1;
47
+
48
+ }
49
+ }
50
+ return a[m][n];
51
+ }
52
+
53
+ int main()
54
+ {
55
+ int t;
56
+ cin >> t;
57
+ while(t--)
58
+ {
59
+ int n,m , i ,j;
60
+ cin >> n>> m;
61
+ string x,b;
62
+ cin >>x>>b;
63
+ int k =1;
64
+ for ( i =1; i< n; i++)
65
+ {
66
+ if( x[i] != x[i-1])
67
+ k++;
68
+ }
69
+ n=k;
70
+ char s[n];
71
+ s[0]=x[0];k=1;
72
+ for ( i =1; i< x.size(); i++)
73
+ {
74
+ if( x[i] != x[i-1])
75
+ {
76
+ s[k]=x[i];
77
+ k++;
78
+ }
79
+ }
80
+
81
+ k =1;
82
+ for ( i =1; i< m; i++)
83
+ {
84
+ if( b[i] != b[i-1])
85
+ k++;
86
+ }
87
+ m=k;
88
+ char str[k];
89
+ str[0]=b[0];k=1;
90
+ for ( i =1; i< b.size(); i++)
91
+ {
92
+ if( b[i] != b[i-1])
93
+ {
94
+ str[k]=b[i];
95
+ k++;
96
+ }
97
+ }
98
+
99
+ int ans = fun(s, str, n, m);
100
+ cout<<ans<<endl;
101
+ }
102
+ return 0;
103
+ }
You can’t perform that action at this time.
0 commit comments