我正在尝试使用逗号分隔的不同字符串组合制作网址,以便我可以使用这些网址执行它们并获取数据。
我简化了这样的内容,我有一个HashSet
,其中包含我的所有字符串,not A,B,C in real
。我只是在这里修改它以使它变得简单。
Set<String> data = new HashSet<String>();
h.add("A");
h.add("B");
h.add("C");
for (int i = 1; i < 1000; i++) {
String pattern = generateString(data);
String url = "http://localhost:8080/service/USERID=101556000/"+pattern;
System.out.println(url);
}
/**
* Below is the method to generate Strings.
/
private String generateString(HashSet<String> data) {
//not sure what logic I am supposed to use here?
}
所以输出应该是这样的 -
http://localhost:8080/service/USERID=101556000/A
http://localhost:8080/service/USERID=101556000/B
http://localhost:8080/service/USERID=101556000/C
http://localhost:8080/service/USERID=101556000/A,B,C
http://localhost:8080/service/USERID=101556000/B,C
http://localhost:8080/service/USERID=101556000/C,A
--
And other combinations
以上输出也可以是任何随机顺序。但它应该是所有可能的组合。如果所有可能的组合都完成,那么重新开始。
有任何建议我如何实现上述问题定义?
答案 0 :(得分:3)
你问的不是微不足道的。
让我们看看2个字符串,A和B.
以下是所有排列。
A
B
AB
BA
好的,让我们看看3个字符串,A,B和C.
以下是所有排列。
A
B
C
AB
AC
BA
BC
CA
CB
ABC
ACB
BAC
BCA
CAB
CBA
你看到了一种模式吗?
首先,您必须找到所有单个字符串排列。然后是两个字符串排列。然后是三串排列。依此类推,最多可达字符串数量。
然后,在一组排列中(比如两个字符串集),你必须找到所有可能的排列。
您可以使用java循环执行此操作。你也可以使用递归。
答案 1 :(得分:2)
考虑到什么是k-排列(http://en.wikibooks.org/wiki/Probability/Combinatorics),你正在寻找k-排列,其中k从1到D变化,其中D是数据集合的大小。
这意味着计算 - 我的第一篇文章我无法发布图片,所以看看位于的公式:
为了做到这一点,你可以使k变化,并且每个k可以变化(即仅处理子数组或数据以枚举k-排列)。通过使用递归将数组向右和向左移动,可以找到这些k置换。
这是一个快速的引导程序,证明需要枚举whart:
public class EnumUrl {
private Set<String> enumeration = null;
private List<String> data = null;
private final String baseUrl = "http://localhost:8080/service/USERID=101556000/";
public EnumUrl(List<String> d) {
data = d;
enumeration = new HashSet<String>(); // choose HashSet : handle duplicates in the enumeration process
}
public Set<String> getEnumeration() {
return enumeration;
}
public static void main(String[] args) {
List<String> data = new ArrayList<String>();
data.add("A");
data.add("B");
data.add("C");
EnumUrl enumerator = new EnumUrl(data);
for (int k = 0; k < data.size(); k++) {
// start from any letter in the set
for (int i = 0; i < data.size(); i++) {
// enumerate possible url combining what's on the right of indice i
enumerator.enumeratePossibleUrlsToRight(data.get(i), i);
// enumerate possible url combining what's on the left of indice i
enumerator.enumeratePossibleUrlsToLeft(data.get(i), i);
}
// make a circular permutation of -1 before the new iteration over the newly combined data
enumerator.circularPermutationOfData();
}
// display to syso
displayUrlEnumeration(enumerator);
}
private void circularPermutationOfData() {
String datum = data.get(0);
for (int i = 1; i < data.size(); i++) {
data.set(i - 1, data.get(i));
}
data.set(data.size() - 1, datum);
}
private static void displayUrlEnumeration(EnumUrl enumerator) {
for (String url : enumerator.getEnumeration()) {
System.out.println(url);
}
}
private void enumeratePossibleUrlsToRight(String prefix, int startAt) {
enumeration.add(baseUrl + prefix);
if (startAt < data.size() - 1) {
startAt++;
for (int i = startAt; i < data.size(); i++) {
int x = i;
enumeratePossibleUrlsToRight(prefix + "," + data.get(x), x);
}
}
}
private void enumeratePossibleUrlsToLeft(String prefix, int startAt) {
enumeration.add(baseUrl + prefix);
if (startAt > 0) {
startAt--;
for (int i = startAt; i >= 0; i--) {
int x = i;
enumeratePossibleUrlsToLeft(prefix + "," + data.get(x), x);
}
}
}
}
{A,B,C}的节目输出:
http://localhost:8080/service/USERID=101556000/B,C
http://localhost:8080/service/USERID=101556000/B,A,C
http://localhost:8080/service/USERID=101556000/B,C,A
http://localhost:8080/service/USERID=101556000/B,A
http://localhost:8080/service/USERID=101556000/C
http://localhost:8080/service/USERID=101556000/B
http://localhost:8080/service/USERID=101556000/C,B,A
http://localhost:8080/service/USERID=101556000/A,C,B
http://localhost:8080/service/USERID=101556000/A,C
http://localhost:8080/service/USERID=101556000/A,B
http://localhost:8080/service/USERID=101556000/A,B,C
http://localhost:8080/service/USERID=101556000/A
http://localhost:8080/service/USERID=101556000/C,B
http://localhost:8080/service/USERID=101556000/C,A
http://localhost:8080/service/USERID=101556000/C,A,B
对于{A,B,C,D}:
http://localhost:8080/service/USERID=101556000/B,A,D,C
http://localhost:8080/service/USERID=101556000/C,D
http://localhost:8080/service/USERID=101556000/A,D,C,B
http://localhost:8080/service/USERID=101556000/A,C,D
http://localhost:8080/service/USERID=101556000/D
http://localhost:8080/service/USERID=101556000/C
http://localhost:8080/service/USERID=101556000/A,C,B
http://localhost:8080/service/USERID=101556000/B
http://localhost:8080/service/USERID=101556000/A,B,C,D
http://localhost:8080/service/USERID=101556000/A,B,C
http://localhost:8080/service/USERID=101556000/D,C,B,A
http://localhost:8080/service/USERID=101556000/C,B,A,D
http://localhost:8080/service/USERID=101556000/A,B,D
http://localhost:8080/service/USERID=101556000/D,B
http://localhost:8080/service/USERID=101556000/D,C
http://localhost:8080/service/USERID=101556000/A
http://localhost:8080/service/USERID=101556000/D,C,A
http://localhost:8080/service/USERID=101556000/D,C,B
http://localhost:8080/service/USERID=101556000/C,D,A
http://localhost:8080/service/USERID=101556000/C,D,B
http://localhost:8080/service/USERID=101556000/D,A
http://localhost:8080/service/USERID=101556000/A,D,C
http://localhost:8080/service/USERID=101556000/A,D,B
http://localhost:8080/service/USERID=101556000/C,B,D
http://localhost:8080/service/USERID=101556000/B,A,D
http://localhost:8080/service/USERID=101556000/B,C
http://localhost:8080/service/USERID=101556000/B,A,C
http://localhost:8080/service/USERID=101556000/B,C,A
http://localhost:8080/service/USERID=101556000/B,A
http://localhost:8080/service/USERID=101556000/B,C,D
http://localhost:8080/service/USERID=101556000/C,B,A
http://localhost:8080/service/USERID=101556000/A,D
http://localhost:8080/service/USERID=101556000/D,A,B
http://localhost:8080/service/USERID=101556000/A,C
http://localhost:8080/service/USERID=101556000/D,A,C
http://localhost:8080/service/USERID=101556000/B,C,D,A
http://localhost:8080/service/USERID=101556000/A,B
http://localhost:8080/service/USERID=101556000/B,D
http://localhost:8080/service/USERID=101556000/C,D,A,B
http://localhost:8080/service/USERID=101556000/D,A,B,C
http://localhost:8080/service/USERID=101556000/D,B,A
http://localhost:8080/service/USERID=101556000/D,B,C
http://localhost:8080/service/USERID=101556000/B,D,A
http://localhost:8080/service/USERID=101556000/C,B
http://localhost:8080/service/USERID=101556000/C,A,D
http://localhost:8080/service/USERID=101556000/C,A
http://localhost:8080/service/USERID=101556000/B,D,C
http://localhost:8080/service/USERID=101556000/C,A,B
这不是详尽的枚举。基本上我们应该有:
(我的第一篇文章我无法发布图片来查看我的回复中的等式,我没有发布2个链接的声誉... #omg)
这使得64个组合,分布如下:
你可以看到我的程序适用于k = 1,k = 2和k = 3。但是对于k = 4,没有24个组合。为了完成程序,您还需要迭代其他类型的数据,而不是循环置换。实际上当k = 4时,循环置换不会产生例如ADBC作为输入数据(因此例如我的实现不能生成DBCA)。在这种情况下,您需要以所有可能的顺序枚举所有可能的数据输入数组,其中包含n个元素。这是k-置换的特殊情况,其中k = n,因此导致找到n!
置换。我们可以通过为每个n!
可能的排列调用EnumUrl方法来实现这一点。
为此,您应该相应地更新EnumUrl enumerator = new EnumUrl(data);
,但我想我正在为您做一些工作: - )
HTH
答案 2 :(得分:1)
适用于任意设置大小的简短版本,使用泛型,使用guava和排列方法given here。
基本上这个想法如下:
对于每组powerset,生成所有排列
public class QuickEnumeration {
Set<T> objects;
public QuickEnumeration(Set<T> objects) {
this.objects = objects;
}
public List<List<T>> generateEnumeration() {
List<List<T>> result = new ArrayList<List<T>>();
// Compute the powerset
Set<Set<T>> powerset = Sets.powerSet(objects);
for (Set<T> set : powerset) {
// Discard empty set
if (set.size() > 0) {
// Arraylist faster for swapping
ArrayList<T> start = new ArrayList<T>(set);
permute(start, 0, result);
}
}
return result;
}
private void permute(ArrayList<T> arr, int k, List<List<T>> result) {
for (int i = k; i < arr.size(); i++) {
java.util.Collections.swap(arr, i, k);
permute(arr, k + 1, result);
java.util.Collections.swap(arr, k, i);
}
if (k == arr.size() - 1) {
result.add((List<T>) arr.clone());
}
}
public static void main(String[] args) {
Set<String> testSet = new HashSet<>();
testSet.add("A");
testSet.add("B");
testSet.add("C");
QuickEnumeration<String> enumerate = new QuickEnumeration<>(testSet);
System.out.println(enumerate.generateEnumeration());
}
}
用“A”,“B”,“C”进行测试给出:
[[A], [B], [A, B], [B, A], [C], [A, C], [C, A], [B, C], [C, B], [A, B, C], [A, C, B], [B, A, C], [B, C, A], [C, B, A], [C, A, B]]
答案 3 :(得分:0)
我不完全确定你真正想要的是什么,所以我最终为你编写了这段代码。希望它能让你开始!
public static void doThis() {
String url1="http://www.example.com";
String string1="A";
String url2="http://www.foo.com";
String string2="B";
String url3="http://www.bar.com";
String string3="C";
Map<String, String> abbrMap = new HashMap<String, String>();
abbrMap.put(string1, url1);
abbrMap.put(string2, url2);
abbrMap.put(string3, url3);
String string = string1+string2+string3;
for(Map.Entry<String, String> m : abbrMap.entrySet()) {
arrange(string, m.getValue());
}
}
private static void arrange(String str, String url) {
if (str.length()==0) return;
StringBuffer sbuf = new StringBuffer();
for (int j=0; j<str.length(); j++) {
for(int i=j; i<str.length(); i++) {
char c = str.charAt(i);
sbuf.append(c);
System.out.println(url+"/"+sbuf.toString());
sbuf.append(",");
}
sbuf.setLength(0);
}
}
输出:
http://www.example.com/A
http://www.example.com/A,B
http://www.example.com/A,B,C
http://www.example.com/B
http://www.example.com/B,C
http://www.example.com/C
http://www.foo.com/A
http://www.foo.com/A,B
http://www.foo.com/A,B,C
http://www.foo.com/B
http://www.foo.com/B,C
http://www.foo.com/C
http://www.bar.com/A
http://www.bar.com/A,B
http://www.bar.com/A,B,C
http://www.bar.com/B
http://www.bar.com/B,C
http://www.bar.com/C