/// Header file begin
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <cmath>
#include <cctype>
#include <sstream>
#include <set>
#include <list>
#include <stack>
#include<utility>
#include <queue>
#include <algorithm>
#include<cstdlib>
/// End
//..........
/// Macro
#define sf scanf
#define pf printf
#define sfint(a,b) scanf("%d %d",&a,&b)
#define sfl(a,b) scanf("%ld %ld",&a,&b)
#define sfll(a,b) scanf("%lld %lld",&a,&b)
#define sfd(a,b) scanf("%lf %lf",&a,&b)
#define sff(a,b) scanf("%f %f",&a,&b)
#define lp1(i,n) for(i=0;i<n;i++)
#define lp2(i,n) for(i=1;i<=n;i++)
#define mem(c,v) memset(c,v,sizeof(c))
#define cp(a) cout<<" "<<a<<" "
#define nl puts("")
#define sq(x) ((x)*(x))
#define all(x) x.begin(),x.end()
#define reall(x) x.rbegin(),x.rend()
#define sz size()
#define gc getchar()
#define pb push_back
#define freader freopen("input.txt","r",stdin)
/// End.........
/// Size
#define mx7 20000100
#define mx6 1500000
#define mx5 100005
#define mx4 1000100
#define inf 1<<30 //infinity value
#define eps 1e-9
#define mx 1009
#define mod 1000000007
#define pi acos(-1.0)
/// Macros for Graph
#define up 1
#define left 2
#define diag 3
#define nil 0
using namespace std;
/***************/
/// typedef
typedef long long LL;
typedef long L;
typedef unsigned long long ull;
typedef unsigned long ul;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef vector<int>vi;
typedef vector<long long> vll;
typedef vector<long>vl;
typedef vector<char>vch;
typedef vector<string>vs;
typedef map<int,int>mpii;
typedef map<int,bool>mpbi;
typedef map<long,bool>mpbl;
typedef map<long long,bool>mpbll;
typedef map<char,int>mpci;
typedef map<char,bool>mpbc;
typedef map<string,int>mpsi;
typedef map<long long,long long>mpll;
/// template
template<class T> T gcd(T a, T b ) {return b<=0?a:gcd(b,a%b);}
template<class T> T large(T a, T b ) {return a>b?a:b;}
template<class T> T small(T a, T b ) {return a<b?a:b;}
template<class T> T diffrnce(T a, T b) {return a-b<0?b-a:a-b;}
vs x,y,lcs;
int c[mx][mx],b[mx][mx];
void display(int i,int j)
{
if(i==0 or j==0)
{
return;
}
if(b[i][j] == diag)
{
display(i-1,j-1);
//cout << x[i-1]<<" ";
lcs.push_back(x[i-1]);
}
else if(b[i][j] == up)
{
display(i-1,j);
}
else
{
display(i,j-1);
}
}
void build_LCS(int m,int n)
{
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(x[i-1] == y[j-1])
{
c[i][j] = c[i-1][j-1]+1;
b[i][j] = diag;
}
else if(c[i-1][j] >= c[i][j-1])
{
c[i][j] = c[i-1][j];
b[i][j] = up;
}
else
{
c[i][j] = c[i][j-1];
b[i][j] = left;
}
}
}
display(m,n);
}
int main()
{
char ch[mx];
while(~sf("%s",&ch))
{
x.clear();
y.clear();
lcs.clear();
string s=ch,s1,s2;
x.push_back(s);
while(cin >> s1 and s1!="#")
{
s=s1;
x.push_back(s);
}
int xlen=x.size();
//for(int i=0;i<xlen;i++)cout << x[i] << " "; nl;
while(cin >> s2 and s2!="#")
{
s=s2;
y.push_back(s);
}
int ylen=y.size();
//for(int i=0;i<ylen;i++)cout << y[i] <<" "; nl;
build_LCS(x.size(),y.size());
int sp=lcs.size(),lcslen=lcs.size();
for(int i=0;i<lcslen;i++)
{
cout << lcs[i];
if(sp>1)
{
cout << " ";
sp--;
}
}
nl;
}
return 0;
}
Comments
Post a Comment