最长公共子序列
最长公共子序列
http://www.nowcoder.com/questionTerminal/6d29638c85bb4ffd80c020fe244baf11
import java.util.*;
public class Solution {
/**
* longest common subsequence
* @param s1 string字符串 the string
* @param s2 string字符串 the string
* @return string字符串
*/
public static String LCS (String s1, String s2) {
int[][] count = new int[s1.length()+1][s2.length()+1];
StringBuilder maxString = new StringBuilder();
for (int i = 0 ; i < s1.length() ;i++){
for (int j = 0 ; j < s2.length() ; j++){
if (s1.charAt(i) == s2.charAt(j)){
count[i+1][j+1] = count[i][j] +1;
}else {
count[i+1][j+1] = Math.max(count[i+1][j],count[i][j+1]);
}
}
}
int index =1,indexRow =1,indexCol = 1;
// indexRow indexCol记录下添加到maxString字符的下标,下次添加只能大于这个下标
for (int i = 1 ; i <= s1.length() ;i++){
for (int j = 1 ; j <= s2.length() ; j++){
if (index == count[i][j] && indexCol < i && indexRow < j){
maxString.append(s2.charAt(j-1));
indexRow = j;
indexCol = i;
index++;
}
}
}
return maxString.toString();
// write code here
}
}
传音控股公司福利 319人发布
