有没有办法把几百个上千个字母组成的数列,把相似的末端和首端连接起来组成长数列  也就是说 abc acd deb cba 这样的数组,最后连接成
abc cba acd deb 这样的连续首位相连的数组,连续越多越好。
求算法

解决方案 »

  1.   

    写了大半天,终于测试通过,敬请大家斧正!!!数据量较小,如楼主有大量数据请测试下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());
    }
    }