急求多个子串的最长公共子序列
求多个子串的最长公共子序列,不需要连续,返回运行时间、公共子序列和子序列长度需用java实现,需要用两种方式测试
1)5个串,串长增加,如 50、60、70
2)串长固定(60),增加串数 --------------------编程问答-------------------- 公共子序列,不连续? --------------------编程问答-------------------- 不连续,就是包含该字符就可以了
8知道是不是这个意思?
public static Object[] commonString(String[] source) {--------------------编程问答-------------------- 是的,可以不连续,谢谢各位帮忙的人们了,灰常感谢 --------------------编程问答-------------------- 还有,能不能加入Java的并发机制呢?
Object[] result = new Object[3];
if (source==null || source.length == 0) {
result[0] = new Integer(0);
return result;
} else if (source.length==1) {
result[0] = new Integer(0);
result[1] = source[0];
result[2] = new Integer(source[0].length);
return result;
}
lont time1 = System.currentTimeMillis();
String s = source[0];
StringBuilder buf = new StringBuilder();
for (int i=1; i<source.length; i++) {
for (char[] c : source[i].toCharArray()) {
if (s.indexOf(c) >= 0) buf.append(c);
}
s = buf.toString();
buf.delete(0, buf.length());
}
long time2 = System.currentTimeMillis();
result[0] = new Integer((int)(time2-time1));
result[1] = s;
result[2] = new Integer(s.length());
return result;
}
//test 1
char[] c = "abcdefghijklmnopqrstuvwxyz".toString();
int count = c.length;
int[] len = {50, 60, 70, 80, 90};
String[] src = new String[len.length];
StringBuilder buf = new StringBuilder();
for (int i=0; i<len.length; i++) {
for (int j=0; j<len[i]; j++)
buf.append(c[(int)(Math.random()*count)]);
src[i] = buf.toString();
buf.delete(0, buf.length());
}
for (Object o : commonString(src)) {
System.out.println(o);
}
//test 2
char[] c = "abcdefghijklmnopqrstuvwxyz".toString();
int count = c.length;
int len = 60;
int srcCount = 10;
String[] src = new String[srcCount];
StringBuilder buf = new StringBuilder();
for (int i=0; i<srcCount; i++) {
for (int j=0; j<len; j++)
buf.append(c[(int)(Math.random()*count)]);
src[i] = buf.toString();
buf.delete(0, buf.length());
}
for (Object o : commonString(src)) {
System.out.println(o);
}
补充:Java , Eclipse