//计算两个序列的LCS长度和序列,例如x为{A,B,C,B,D,A,B} y为{B,D,C,A,B,A}
public static void calcLcs(ArrayList<Character>x,ArrayList<Character>y){
int m = x.size();
int n = y.size();
int[][] b = new int[m+1][n+1];
int [][]c = new int[m+1][n+1];
for(int i=1;i<=m;++i){
c[i][0] = 0;
}
for(int j =0;j<=n;++j){
c[0][j] = 0;
}
for(int i=1;i<=m;++i)
for(int j =1;j<=n;++j){
if(x.get(i-1)==y.get(j-1)){
c[i][j]=c[i-1][j-1]+1;
b[i][j]=1;
}
else if(c[i-1][j]>=c[i][j-1]){
c[i][j]=c[i-1][j];
b[i][j]=2;
}
else {
c[i][j] = c[i][j-1];
b[i][j]=3;
}
}
System.out.println(c[m][n]);
System.out.println(lcstring(b,m,n,x));
}
public static String lcstring(int[][]b,int i,int j,ArrayList<Character>x ){
String s="";
while(i!=0&&j!=0){
if(b[i][j]==1){
s=x.get(i-1)+s;
i=i-1;
j=j-1;
}
if(b[i][j]==2){
i=i-1;
}
if(b[i][j]==3){
j=j-1;
}
}
return s;
}