这个关于分配律的算法问题该怎么解决?

code-artist 2006-03-05 09:13:21
用程序实现以下:
如果某表List中存放着若干个(个数不定)子数组表ArrayList,把这些子表中的元素(子表中元素个数不定)按分配律分开:
例如:
List={ {a,b},{c,}, {a,e} }
把他门拆开后结果为
{ {a,c},{a,c,e},{b,c,a},{b,c,e} }
问题是 总表和子表中元素个数总是变化的(1个,2个或多个)

想了很久没想出解决办法来?希望高手帮忙(是不是要用树型或图结构?)
...全文
184 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wizardblue 2006-03-09
  • 打赏
  • 举报
回复

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Allocation {

public static void main(String[] args) {
List srcList = getTestData();
show(srcList, "--data before allocation--");
List dstList = solve(srcList);
show(dstList, "--data after allocation--");
}

public static void show(List list, String s) {
System.out.println(s);
for (int i = 0; i < list.size(); i++) {
Collection c = (Collection) list.get(i);
show(c.toArray());
}
}

public static void show(Object[] o) {
System.out.print("{");
for (int i = 0; i < o.length; i++) {
System.out.print(o[i]);
if (i + 1 != o.length)
System.out.print(",");
}
System.out.println("}");
}

public static List solve(List srcList) {
if (srcList == null)
throw new NullPointerException();
List tempList = new ArrayList();
tempList.add(new HashSet());
while (srcList.size() > 0) {
List tList = (List) srcList.get(0);
tempList = Allocate(tempList, tList);
srcList.remove(0);
}

return tempList;
}

public static List getTestData() {
String[][] s = new String[][] { new String[] { "a", "b" },
new String[] { "c", }, new String[] { "a", "e" } };

List list = new ArrayList();
for (int i = 0; i < s.length; i++) {
List subList = new ArrayList();
for (int j = 0; j < s[i].length; j++) {
subList.add(s[i][j]);
}
list.add(subList);
}
return list;
}

public static List Allocate(List dst, List src) {
List tempList = new ArrayList();
int index = 0;
do {
for (int j = 0; j < src.size(); j++) {
Set s = new HashSet((Set) dst.get(index));
s.add(src.get(j));
tempList.add(s);
}
index++;
} while (index < dst.size());
return tempList;
}

}
leekooqi 2006-03-09
  • 打赏
  • 举报
回复
强。呵呵。。。
andycpp 2006-03-09
  • 打赏
  • 举报
回复
public class Test {

public static void assemble(ListIterator<String> it, ArrayList<String> ts) {
String s;
boolean flag;
if(it.hasNext()) {
s = it.next();
for(int i=0; i<s.length(); i++) {
flag = ts.contains(s.substring(i, i+1));
if(!flag) ts.add(s.substring(i, i+1));
assemble(it, ts);
if(!flag) ts.remove(ts.size()-1);
}
it.previous();
}else{
for(Iterator it1 = ts.iterator(); it1.hasNext(); )
System.out.print((String)it1.next());
System.out.println("");
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub

ArrayList<String> a = new ArrayList<String>();
a.add("ab");
a.add("c");
a.add("ae");
ArrayList<String> ts = new ArrayList<String>();
assemble(a.listIterator(), ts);



// System.out.println(cb);

}

}
code-artist 2006-03-08
  • 打赏
  • 举报
回复
不好意思,没讲清楚。
原来分步进行可以解决,谢谢你的答案。
wizardblue 2006-03-05
  • 打赏
  • 举报
回复
楼主你是不是还有一些规则没讲清楚咧?
按照分配律的话
List={ {a,b},{c,}, {a,e} }
-》{{a,c},{b,c},{a,e}}
-> {{a,c,a},{a,c,e},{b,c,a},{b,c,e}
而不是你说的
{ {a,c},{a,c,e},{b,c,a},{b,c,e} }
假如是重复的要去掉的话,至少你也应该说清楚吧?
universe01 2006-03-05
  • 打赏
  • 举报
回复
关注!
code-artist 2006-03-05
  • 打赏
  • 举报
回复
没有规律,元素个数随机产生的?
如果能有递归实现,那么应该能有栈实现吧。
我再想想
qiujun1 2006-03-05
  • 打赏
  • 举报
回复
变化有规律么?
可能要用到递归

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧