#include<bits/stdc++.h>
using namespace std ;
#define ll long long int 
#define pb push_back 

    pair<ll , ll> ss[3000][3000] ;
int main()
{
    string a ,b ;
    cin >>a>>b ;
    ll arr[a.size() ][b.size()] ;
    ll brr[a.size() ][b.size()] ;
    
    for(ll i =0 ; i < a.size() ; i++)
    {
        for(ll j =0 ; j < b.size() ; j++)
        {
            brr[i][j] = 0;
       
        }
    }
    stack<pair<ll , ll> > s  ;
    s.push(make_pair(0 , 0)) ;
    while(!s.empty())
    {
        ll i = s.top().first , j = s.top().second ;
        
        if(brr[i][j]!=1)
        {
        if(a[i] ==b[j])
        {
            if(i< a.size() -1 && j < b.size()-1)
            {
                if(brr[i+1][j+1] == 1)
                {
                    arr[i][j] =1 +arr[i+1][j+1] ;
                    ss[i][j] =  make_pair(i+1,j+1) ;
                    s.pop() ;
                    brr[i][j]=1 ;
                }else
                {
                    s.push(make_pair(i+1, j+1)) ;
            }
        }else
        {
            ss[i][j] = make_pair(i,j) ;
            arr[i][j]= 1 ;
            s.pop() ;
                    brr[i][j]=1 ;
            
        }
    }else
    {
        if(i+1 < a.size() &&  j+1 <b.size())
        {
            if(brr[i+1][j]==1 && brr[i][j+1]==1)
            {
                if(arr[i+1][j] > arr[i][j+1])
                {
                    ss[i][j] = make_pair(i+1,j) ;
                }else
                {
                    ss[i][j] =make_pair(i,j+1); 
                }
                arr[i][j] = max(arr[i+1][j] , arr[i][j+1]) ;
                s.pop() ;
                    brr[i][j]=1 ;
            }else
            {
                s.push(make_pair(i+1 , j)) ;
                s.push(make_pair(i, j+1)) ;
            }
        }else
        {
            if(a.size()-1 > i )
            {
                if(brr[i+1][j]==1)
                {
                    ss[i][j] =make_pair(i+1,j) ;
                    arr[i][j] = arr[i+1][j] ;
                    s.pop() ;
                    brr[i][j]=1 ;
                }else
                {
                    s.push(make_pair(i +1 , j  )) ;
                }
            
            
            }else  if(b.size()-1 >j )
            {
                if(brr[i][j+1]==1)
                {
                    ss[i][j] =make_pair(i,j+1) ;
                    arr[i][j] = arr[i][j+1] ;
                    s.pop() ;
                    brr[i][j]=1 ;
                }else
                {
                    s.push(make_pair(i  , j +1 )) ;
                }
            
            
            }else
            {
                arr[i][j] = 0 ;
                ss[i][j] = make_pair(i,j) ;
                s.pop() ;
                brr[i][j] =1 ;
            }
            
        }
    }
    }else
    s.pop() ;
    
    }
    string e = "" ;
    
    
    for(ll i =0 , j =0 ; ;)
    {
        if(a[i]==b[j])
        e =e+a[i] ;
       if(i==ss[i][j].first && j==ss[i][j].second )
        break ;
         
         ll aa = i  ,  bb = j ;
         i =ss[aa][bb].first ;
         j=ss[aa][bb].second ;
    }
    cout<<e ;
        return 0 ;
    
}

Measured memory_bytes: 281600