- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有 2 个有限集:
Set A: {A, B, C, D, ...}
Set B: {1, 2, 3, 4, ...}
它们可以是相同或不同的长度。我正在尝试将它们配对,以便集合 A 中的每个元素都恰好与集合 B 中的一个元素配对。强>
注意:反之亦然。 集合 B 中的一个元素可以与集合 A 中的零个、一个或所有元素配对。因此,这不是双射。
我已经尝试了几个小时来想出一个算法来列出符合这些规则的所有可能的配对集。我已经尝试过迭代和递归,但似乎都没有太大的进展。
我也看过 this SO Q&A但该解决方案对我不起作用,因为它不允许第二组中的一个元素与第一组中的多个元素配对。
任何方向将不胜感激!
附言万一有人指责我,这不是学校作业。这是我开始的个人项目。
两个有效配对的示例:
1:A
2:C
3:B
4:D
1:A,C
2:D
3:
4:B
编辑:回答!感谢@Vincent
注意:不小心颠倒了域(A 组)和密码域(B 组)的定义。现在,codomain 的每个元素恰好映射到域中的一个元素,但不一定相反。
import java.util.*;
//My third attempt at a solution :)
public class MapStuff3 {
public static void main(String[] args) {
//Sample domain
String[] domainArr = { "A", "B", "C", "D" };
List<String> domain = new ArrayList<String>() {
{
for (String s : domainArr)
add(s);
}
};
//Sample codomain
Integer[] codomainArr = { 1, 2, 3 };
List<Integer> codomain = new ArrayList<Integer>() {
{
for (Integer i : codomainArr)
add(i);
}
};
map(domain, codomain);
}
//Each of codomain maps to exactly one on domain, not necessarily the converse
static <A, B> void map(List<A> domain, List<B> codomain) {
//This is the list of all combinations
List<Map<B, List<A>>> listOfAllMaps = new ArrayList<Map<B, List<A>>>();
Map<B, List<A>> map = new HashMap<B, List<A>>();
listOfAllMaps = map(domain, codomain, map);
int count = 0; //just for fun. count will always go to codomain.size()^domain.size()
ListIterator<Map<B, List<A>>> listIterator = listOfAllMaps.listIterator();
while (listIterator.hasNext()) {
Map<B, List<A>> oneMap = listIterator.next();
//count the number of elements in all of the lists to make sure that every element in the domain is paired to something
int totalSize = 0;
for (B key : oneMap.keySet())
totalSize += oneMap.get(key).size();
//if it is, it's valid
if (totalSize == domain.size())
System.out.println(++count + ": " + oneMap);
else //remove invalid entries
listIterator.remove();
}
//Because we removed invalid entires, listOfAllMaps will now contain the correct answer
}
/**
* Recursive method to list all possible mappings, including ones where not every element in the
* domain is mapped to, layer by layer
*/
static <A, B> List<Map<B, List<A>>> map(List<A> domain, List<B> codomain, Map<B, List<A>> map) {
//Base case
//If every element in the codomain has been mapped, return our map inside of a list
if (codomain.size() == 0) {
List<Map<B, List<A>>> finalAnswer = new ArrayList<Map<B, List<A>>>();
finalAnswer.add(map);
return finalAnswer;
}
//Holds our partial answers
List<Map<B, List<A>>> listOfAllMaps = new ArrayList<Map<B, List<A>>>();
//List of the indices of all subsets of the given domain
List<List<Integer>> indexSuperList = listAll(domain.size());
//For each subset of the given domain
for (List<Integer> indexList : indexSuperList) {
//Make copies of our parameters so they can be mutated without affecting everything else
//I always forget if this is necessary in Java, but I'm too tired too look it up right now. Better safe than sorry.
List<A> workingDomain = new ArrayList<A>(domain);
List<B> workingCodomain = new ArrayList<B>(codomain);
Map<B, List<A>> workingMap = new HashMap<B, List<A>>(map);
//Map all elements in the given subset of the domain to the first element in the given codomain
//Also remove every element in the domain that has been used
workingMap.put(workingCodomain.get(0), new ArrayList<A>());
for (int i : indexList)
workingMap.get(workingCodomain.get(0)).add(workingDomain.remove(i));
//Remove the first element in the given codomain
workingCodomain.remove(0);
//Add on the next layer
listOfAllMaps.addAll(map(workingDomain, workingCodomain, workingMap));
}
return listOfAllMaps; //This will only happen if the next layer of recursion hit the base case. Just return what we have
}
/**
* Lists the indices corresponding to all subsets of a list of a certain size N, including the
* empty set
* Adapted from https://stackoverflow.com/a/7206478/7059012
*/
static List<List<Integer>> listAll(int N) {
List<List<Integer>> allLists = new ArrayList<List<Integer>>();
allLists.add(new ArrayList<Integer>());
int allMasks = (1 << N);
for (int i = 1; i < allMasks; i++) {
List<Integer> oneList = new ArrayList<Integer>();
for (int j = 0; j < N; j++)
if ((i & (1 << j)) > 0) //The j-th element is used
oneList.add(j);
//Put in reverse order so that when we're removing them on lines 88-89, removing the first doesn't invalidate the indices of the others
Collections.sort(oneList, Collections.reverseOrder());
allLists.add(oneList);
}
return allLists;
}
}
打印出来:
1: {1=[], 2=[], 3=[D, C, B, A]}
2: {1=[], 2=[A], 3=[D, C, B]}
3: {1=[], 2=[B], 3=[D, C, A]}
4: {1=[], 2=[B, A], 3=[D, C]}
5: {1=[], 2=[C], 3=[D, B, A]}
6: {1=[], 2=[C, A], 3=[D, B]}
7: {1=[], 2=[C, B], 3=[D, A]}
8: {1=[], 2=[C, B, A], 3=[D]}
9: {1=[], 2=[D], 3=[C, B, A]}
10: {1=[], 2=[D, A], 3=[C, B]}
11: {1=[], 2=[D, B], 3=[C, A]}
12: {1=[], 2=[D, B, A], 3=[C]}
13: {1=[], 2=[D, C], 3=[B, A]}
14: {1=[], 2=[D, C, A], 3=[B]}
15: {1=[], 2=[D, C, B], 3=[A]}
16: {1=[], 2=[D, C, B, A], 3=[]}
17: {1=[A], 2=[], 3=[D, C, B]}
18: {1=[A], 2=[B], 3=[D, C]}
19: {1=[A], 2=[C], 3=[D, B]}
20: {1=[A], 2=[C, B], 3=[D]}
21: {1=[A], 2=[D], 3=[C, B]}
22: {1=[A], 2=[D, B], 3=[C]}
23: {1=[A], 2=[D, C], 3=[B]}
24: {1=[A], 2=[D, C, B], 3=[]}
25: {1=[B], 2=[], 3=[D, C, A]}
26: {1=[B], 2=[A], 3=[D, C]}
27: {1=[B], 2=[C], 3=[D, A]}
28: {1=[B], 2=[C, A], 3=[D]}
29: {1=[B], 2=[D], 3=[C, A]}
30: {1=[B], 2=[D, A], 3=[C]}
31: {1=[B], 2=[D, C], 3=[A]}
32: {1=[B], 2=[D, C, A], 3=[]}
33: {1=[B, A], 2=[], 3=[D, C]}
34: {1=[B, A], 2=[C], 3=[D]}
35: {1=[B, A], 2=[D], 3=[C]}
36: {1=[B, A], 2=[D, C], 3=[]}
37: {1=[C], 2=[], 3=[D, B, A]}
38: {1=[C], 2=[A], 3=[D, B]}
39: {1=[C], 2=[B], 3=[D, A]}
40: {1=[C], 2=[B, A], 3=[D]}
41: {1=[C], 2=[D], 3=[B, A]}
42: {1=[C], 2=[D, A], 3=[B]}
43: {1=[C], 2=[D, B], 3=[A]}
44: {1=[C], 2=[D, B, A], 3=[]}
45: {1=[C, A], 2=[], 3=[D, B]}
46: {1=[C, A], 2=[B], 3=[D]}
47: {1=[C, A], 2=[D], 3=[B]}
48: {1=[C, A], 2=[D, B], 3=[]}
49: {1=[C, B], 2=[], 3=[D, A]}
50: {1=[C, B], 2=[A], 3=[D]}
51: {1=[C, B], 2=[D], 3=[A]}
52: {1=[C, B], 2=[D, A], 3=[]}
53: {1=[C, B, A], 2=[], 3=[D]}
54: {1=[C, B, A], 2=[D], 3=[]}
55: {1=[D], 2=[], 3=[C, B, A]}
56: {1=[D], 2=[A], 3=[C, B]}
57: {1=[D], 2=[B], 3=[C, A]}
58: {1=[D], 2=[B, A], 3=[C]}
59: {1=[D], 2=[C], 3=[B, A]}
60: {1=[D], 2=[C, A], 3=[B]}
61: {1=[D], 2=[C, B], 3=[A]}
62: {1=[D], 2=[C, B, A], 3=[]}
63: {1=[D, A], 2=[], 3=[C, B]}
64: {1=[D, A], 2=[B], 3=[C]}
65: {1=[D, A], 2=[C], 3=[B]}
66: {1=[D, A], 2=[C, B], 3=[]}
67: {1=[D, B], 2=[], 3=[C, A]}
68: {1=[D, B], 2=[A], 3=[C]}
69: {1=[D, B], 2=[C], 3=[A]}
70: {1=[D, B], 2=[C, A], 3=[]}
71: {1=[D, B, A], 2=[], 3=[C]}
72: {1=[D, B, A], 2=[C], 3=[]}
73: {1=[D, C], 2=[], 3=[B, A]}
74: {1=[D, C], 2=[A], 3=[B]}
75: {1=[D, C], 2=[B], 3=[A]}
76: {1=[D, C], 2=[B, A], 3=[]}
77: {1=[D, C, A], 2=[], 3=[B]}
78: {1=[D, C, A], 2=[B], 3=[]}
79: {1=[D, C, B], 2=[], 3=[A]}
80: {1=[D, C, B], 2=[A], 3=[]}
81: {1=[D, C, B, A], 2=[], 3=[]}
最佳答案
我建议您在集合 A 上使用递归处理:枚举所有可以在第一个元素上完成的配对,并将其与集合 A 的其余部分和集合 B 的其余部分的枚举相结合。
在集合 A 的递归结束时,您可以像在问题中所做的那样按集合 B 中的元素对对进行分组。
关于arrays - 列出两组拟合标准的所有配对的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45442239/
我最近在读 CSAPP。在 10.9 节中,它说标准 I/O 不应该与 socket 一起使用,原因如下: (1) The restrictions of standard I/O Restricti
似乎是一个足够标准的问题,可以保证解决方案中的标准设计: 假设我想在文件中写入 x+2(或更少)个字符串。 x 字符串构成一个部分的内容,这两个字符串构成该部分的页眉和页脚。要注意的是,如果内容中没有
代码版本管理 在项目中,代码的版本管理非常重要。每个需求版本的代码开发在版本控制里都应该经过以下几个步骤。 在master分支中拉取该需求版本的两个分支,一个feature分支,
我有以下sql查询,我需要获取相应的hibernate条件查询 SELECT COUNT(DISTINCT employee_id) FROM erp_hr_payment WHERE payment
所以我正在编写一些代码,并且最近遇到了实现一些 mixin 的需要。我的问题是,设计混音的正确方法是什么?我将使用下面的示例代码来说明我的确切查询。 class Projectile(Movable,
我的环境变量包含如下双引号: $echo $CONNECT_SASL_JAAS_CONFIG org.apache.kafka.common.security.plain.PlainLoginModu
示例: /** * This function will determine whether or not one string starts with another string. * @pa
有没有办法在 Grails 中做一个不区分大小写的 in 子句? 我有这个: "in"("name", filters.tags) 我希望它忽略大小写。我想我可以做一个 sqlRestriction
我搜索了很长时间,以查找将哪些boost库添加到std库中,但是我只找到了一个新库的完整列表(如此处:http://open-std.org/jtc1/sc22/wg21/docs/library_t
我已经通过使用这个肮脏的黑客解决了我的问题: ' Filter managerial functions ActiveSheet.Range("$A$1:$BW$2211").Auto
因此,我很难理解我需要遵循的标准,以便我的 Java 程序能够嵌入 HTML。我是否只需将我的主类扩展到 Applet 类,或者我还需要做更多的事情吗?另外,在我见过的每个 Applet 示例中,它都
我对在 Hibernate 中使用限制有疑问。 我必须创建条件,设置一些限制,然后选择日期字段最大值的记录: Criteria query = session.createCriteria(Stora
我有标准: ICriteria criteria = Session.CreateCriteria() .SetFetchMode("Entity1", FetchMo
我很难编写条件来选择所有子集合或孙集合为空的实体。我可以将这些作为单独的条件来执行,但我无法将其组合成一个条件。 类结构: public class Component { p
@Entity class A { @ManyToMany private List list; ... } @Entity class B { ... } 我想使用条件(不是 sql 查询)从 A
我的数据库中有以下表结构: Table A: Table B: Table C: _______________
请帮助我: 我有下一张 table : 单位 ID 姓名 用户 ID 姓名 利率 单位 ID 用户 ID 我不明白如何从 SQL 创建正确的条件结构: 代码: SELECT * FROM Unit W
我正在构建一个包含项目的网站,每个项目都有一个页面,例如: website.com/book/123 website.com/film/456 website.com/game/789 每个项目都可以
我需要使用两个属性的组合来过滤结果列表。一个简单的 SQL 语句如下所示: SELECT TOP 10 * FROM Person WHERE FirstName + ' ' + LastName L
我有一个“ super 实体”SuperEntity 和三个扩展父类(super class)的实体 ChildEntity1、...、ChildEntity3。 搜索数据库中的所有实体很容易,即我们
我是一名优秀的程序员,十分优秀!