1
+ /*
2
+ * Note: This template uses some c++11 functions , so you have to compile it with c++11 flag.
3
+ * Example:- $ g++ -std=c++11 c++Template.cpp
4
+ *
5
+ * Author : Harsh Gupta
6
+ * Handle: harshsavasil
7
+ *
8
+ */
9
+
10
+ /*
11
+ Problem Description: Longest Common Subsequence
12
+ Given two sequences, find the length of longest subsequence present in both of them.
13
+ A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous.
14
+ For example, “abc”, “abg”, “bdf”, “aeg”, ‘”acefg”, .. etc are subsequences of “abcdefg”.
15
+ So a string of length n has 2^n-1 different possible subsequences.
16
+
17
+ Tags: Dynamic Programming, Recursion
18
+
19
+ Sample Input -
20
+ 1
21
+ AGGTAB GXTXAYB
22
+
23
+ Sample Output -
24
+ 4
25
+ TC - O(n^2)
26
+ SC - O(n^2)
27
+ */
28
+ #include < iostream>
29
+ #include < sstream>
30
+ #include < cstdio>
31
+ #include < cmath>
32
+ #include < cstring>
33
+ #include < cctype>
34
+ #include < string>
35
+ #include < vector>
36
+ #include < list>
37
+ #include < set>
38
+ #include < map>
39
+ #include < queue>
40
+ #include < stack>
41
+ #include < algorithm>
42
+ #include < functional>
43
+
44
+ using namespace std ;
45
+
46
+ #define DEBUG (x ) cout << ' >' << #x << ' :' << x << endl;
47
+ #define REP (i, n ) for (int i = 0 ; i < (n); i++)
48
+ #define FOR (i, a, b ) for (int i = (a); i <= (b); i++)
49
+ #define FORD (i, a, b ) for (int i = (a); i >= (b); i--)
50
+ inline bool EQ (double a, double b)
51
+ {
52
+ return fabs (a - b) < 1e-9 ;
53
+ }
54
+ const int INF = 1 << 29 ;
55
+ typedef long long ll;
56
+ typedef pair<int , int > ii;
57
+ typedef vector<ii> vii;
58
+ typedef vector<int > vi;
59
+ typedef vector<vector<int >> vvi;
60
+ inline int two (int n) { return 1 << n; }
61
+ inline int test (int n, int b) { return (n >> b) & 1 ; }
62
+ inline void set_bit (int &n, int b) { n |= two (b); }
63
+ inline void unset_bit (int &n, int b) { n &= ~two (b); }
64
+ inline int last_bit (int n) { return n & (-n); }
65
+ inline int ones (int n)
66
+ {
67
+ int res = 0 ;
68
+ while (n && ++res)
69
+ n -= n & (-n);
70
+ return res;
71
+ }
72
+ template <class T >
73
+ void chmax (T &a, const T &b) { a = max (a, b); }
74
+ template <class T >
75
+ void chmin (T &a, const T &b) { a = min (a, b); }
76
+
77
+ // ///////////////////////////////////////////////////////////////////
78
+ void display (const vvi data)
79
+ {
80
+ int row = data.size ();
81
+ int column = data[0 ].size ();
82
+ for (int i = 0 ; i < row; i++)
83
+ {
84
+ for (int j = 0 ; j < column; j++)
85
+ {
86
+ cout << data[i][j] << ' ' ;
87
+ }
88
+ cout << endl;
89
+ }
90
+ cout << endl;
91
+ }
92
+
93
+ int main ()
94
+ {
95
+ freopen (" input.txt" , " r" , stdin);
96
+ freopen (" output.txt" , " w" , stdout);
97
+ int tc;
98
+ cin >> tc;
99
+ while (tc--)
100
+ {
101
+ // cout << 'Case ' << tc << endl;
102
+ string a, b;
103
+ cin >> a >> b;
104
+ int a_size = a.size ();
105
+ int b_size = b.size ();
106
+ vvi lcs (a_size + 1 , vi (b_size + 1 , 0 ));
107
+ for (int i = 0 ; i <= a_size; i++)
108
+ {
109
+ for (int j = 0 ; j <= b_size; j++)
110
+ {
111
+ if (i == 0 || j == 0 )
112
+ {
113
+ lcs[i][j] = 0 ;
114
+ }
115
+ else
116
+ {
117
+ if (a[i - 1 ] == b[j - 1 ])
118
+ {
119
+ lcs[i][j] = 1 + lcs[i - 1 ][j - 1 ];
120
+ }
121
+ else
122
+ {
123
+ lcs[i][j] = max (lcs[i - 1 ][j], lcs[i][j - 1 ]);
124
+ }
125
+ }
126
+ }
127
+ }
128
+ display (lcs);
129
+ cout << " Length of LCS : " << lcs[a_size][b_size] << endl;
130
+ }
131
+ return 0 ;
132
+ }
0 commit comments