Problem:
Score : 100 points

Problem Statement

You are given strings s and t.
Find one longest string that is a subsequence of both s and t.

Notes

A subsequence of a string x is the string obtained by removing zero or more characters from x and concatenating the remaining characters without changing the order.

Constraints

s and t are strings consisting of lowercase English letters.

1 \leq |s|, |t| \leq 3000

Input

Input is given from Standard Input in the following format:

s
t

Output

Print one longest string that is a subsequence of both s and t.
If there are multiple such strings, any of them will be accepted.

Sample Input 1

axyb
abyxb

Sample Output 1

axb

The answer is axb or ayb; either will be accepted.

Sample Input 2

aa
xayaz

Sample Output 2

aa

Sample Input 3

a
z

Sample Output 3

The answer is (an empty string).

Sample Input 4

abracadabra
avadakedavra

Sample Output 4

aaadara

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)


#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define F first
#define S second

#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define int long long
#define ll long long

const int N = 3e3 + 5;
int dp[N][N];
string s1, s2, ans;

int rec(int x, int y){
	if(x == s1.size() || y == s2.size())
		return 0;
	if(dp[x][y] != -1)
		return dp[x][y];
	if(s1[x] == s2[y])
		return dp[x][y] = 1 + rec(x + 1, y + 1);
	return dp[x][y] = max(rec(x + 1, y), rec(x, y + 1));
}
	
void solve(){
	memset(dp, -1, sizeof dp);
	cin >> s1 >> s2;
	rec(0, 0);
	int first = 0, second = 0;
	while(first < s1.size() && second < s2.size()){
		if(s1[first] == s2[second]){
			ans += s1[first];
			first++;
			second++;
		}
		else
		if(dp[first][second + 1] >= dp[first + 1][second]){
			second++;
		}
		else
			first++;

	}
	cout << ans << "\n";
}

signed main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t = 1;
	//cin >> t;
	while(t--)
		solve();
	return 0;
}