有没有办法把几百个上千个字母组成的数列,把相似的末端和首端连接起来组成长数列 有没有办法把几百个上千个字母组成的数列,把相似的末端和首端连接起来组成长数列 也就是说 abc acd deb cba 这样的数组,最后连接成abc cba acd deb 这样的连续首位相连的数组,连续越多越好。求算法 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 写了大半天,终于测试通过,敬请大家斧正!!!数据量较小,如楼主有大量数据请测试下package com.question;import java.util.ArrayList;import java.util.Date;import java.util.Iterator;import java.util.List;public class MostCompTest { @SuppressWarnings("unchecked") public static List<String> sort(ArrayList<String> done, List<String>[][] orgin) { List<String>[][] orgin_temp = orgin.clone(); List<String> temp = new ArrayList<String>(); boolean flag=false; if (done == null || done.size() == 0) { for (List<String>[] listArr : orgin_temp) { for (List<String> list : listArr) { if (list != null) { for (Iterator<String> it = list.iterator(); it .hasNext();) { ArrayList<String> done_temp = new ArrayList<String>(); String s = it.next(); done_temp.add(s); it.remove(); List<String> result = sort(done_temp, orgin_temp); if (result != null && temp.size() < result.size()) { temp = result; } flag=true; } } else { continue; } } } if(!flag){ temp=done; } } else { String lastDone = done.get(done.size() - 1); int lastCharIndex = (int) lastDone.charAt(lastDone.length() - 1) - 97; if (orgin_temp[lastCharIndex] != null) { for (List<String> list : orgin_temp[lastCharIndex]) { if (list != null) { for (Iterator<String> it = list.iterator(); it .hasNext();) { ArrayList<String> done_temp = (ArrayList<String>) done.clone(); String s = it.next(); done_temp.add(s); it.remove(); List<String> result = sort(done_temp, orgin_temp); if (result != null && temp.size() < result.size()) { temp = result; } flag = true; } } else { continue; } } } if (!flag) { temp = done; } } return temp; } @SuppressWarnings("unchecked") public static List<String>[][] comp(String[] orgin) { List<String>[][] listArr = new ArrayList[26][26]; if (orgin != null) { for (String s : orgin) { int start = (int) s.charAt(0) - 97; int end = (int) s.charAt(s.length() - 1) - 97; if (listArr[start][end] != null) { listArr[start][end].add(s); } else { List<String> list = new ArrayList<String>(); list.add(s); listArr[start][end] = list; } } } return listArr; } public static void main(String[] args) { Date start = new Date(); List<String>[][] listArr = comp(new String[] { "acd", "deb", "cba", "abc", "acd", "deb", "cba", "abc", "acd", "deb", "cba", "abc", "acd", "deb", "cba", "abc", "acd", "deb", "cba", "abc", "acd", "deb", "cba", "abc", "acd", "deb", "cba", "abc" }); List<String> list = sort(new ArrayList<String>(), listArr); if (list != null) { for (String s : list) { System.out.print(s + " "); } System.out.println(); } System.out.println(new Date().getTime() - start.getTime()); }} java基础问题 关于对象数组的问题? 网页上如何实现用javacomm20读取客户端串口??? 请教JAVA程序调试的问题 Applet显示不出来?急! 【求解】关于类的Class对象的一些疑惑? Image 问题 Java.NET or J#.NET is coming! 那位仁兄能提供些Java原代码给我吗?谢谢,因为兄弟现在学得很迷茫。需要多看点代码——我以为 谭浩强的牌子,不会错吧?我抄了一段小程序,却过不了 这段程序有什么问题? 几道基础的Java题目请教大家:
import java.util.Date;
import java.util.Iterator;
import java.util.List;public class MostCompTest { @SuppressWarnings("unchecked")
public static List<String> sort(ArrayList<String> done,
List<String>[][] orgin) {
List<String>[][] orgin_temp = orgin.clone();
List<String> temp = new ArrayList<String>();
boolean flag=false;
if (done == null || done.size() == 0) {
for (List<String>[] listArr : orgin_temp) {
for (List<String> list : listArr) {
if (list != null) {
for (Iterator<String> it = list.iterator(); it
.hasNext();) {
ArrayList<String> done_temp = new ArrayList<String>();
String s = it.next();
done_temp.add(s);
it.remove();
List<String> result = sort(done_temp, orgin_temp);
if (result != null && temp.size() < result.size()) {
temp = result;
}
flag=true;
}
} else {
continue;
}
}
}
if(!flag){
temp=done;
}
} else {
String lastDone = done.get(done.size() - 1);
int lastCharIndex = (int) lastDone.charAt(lastDone.length() - 1) - 97;
if (orgin_temp[lastCharIndex] != null) {
for (List<String> list : orgin_temp[lastCharIndex]) {
if (list != null) {
for (Iterator<String> it = list.iterator(); it
.hasNext();) {
ArrayList<String> done_temp = (ArrayList<String>) done.clone();
String s = it.next();
done_temp.add(s);
it.remove();
List<String> result = sort(done_temp, orgin_temp);
if (result != null && temp.size() < result.size()) {
temp = result;
}
flag = true;
}
} else {
continue;
}
}
}
if (!flag) {
temp = done;
}
}
return temp;
} @SuppressWarnings("unchecked")
public static List<String>[][] comp(String[] orgin) {
List<String>[][] listArr = new ArrayList[26][26];
if (orgin != null) {
for (String s : orgin) {
int start = (int) s.charAt(0) - 97;
int end = (int) s.charAt(s.length() - 1) - 97;
if (listArr[start][end] != null) {
listArr[start][end].add(s);
} else {
List<String> list = new ArrayList<String>();
list.add(s);
listArr[start][end] = list;
}
}
}
return listArr;
} public static void main(String[] args) {
Date start = new Date();
List<String>[][] listArr = comp(new String[] { "acd", "deb", "cba",
"abc", "acd", "deb", "cba", "abc", "acd", "deb", "cba", "abc",
"acd", "deb", "cba", "abc", "acd", "deb", "cba", "abc", "acd",
"deb", "cba", "abc", "acd", "deb", "cba", "abc" });
List<String> list = sort(new ArrayList<String>(), listArr);
if (list != null) {
for (String s : list) {
System.out.print(s + " ");
}
System.out.println();
}
System.out.println(new Date().getTime() - start.getTime());
}
}