2013-02-15 234 views
0

我想從逗號分隔的字符串的不同組合中創建一個url,以便我可以使用這些url執行它們並獲取數據。使URL與字符串的所有可能的組合

我簡化了這樣的事情,我有一個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

從我可以告訴,一個URL無關您的問題。你有一個Collection作爲輸入,你想排列所有的可能性。有很多關於如何在stackoverflow上做到這一點的文章。 – 2013-02-15 01:30:40

+1

你想排列或組合?無論哪種方式,請隨時使用Google或SO搜索工具。此主題已在前面討論過。 – 2013-02-15 01:45:10

+0

字符串與逗號之間的任何特定組合。因此,我可以將這些插入網址 – AKIWEB 2013-02-15 01:45:42

回答

2

鑑於什麼是K-佈置(http://en.wikibooks.org/wiki/Probability/Combinatorics),你正在尋找的k排列,其中k從1變化到d,其中d是所述數據的大小集合。

這意味着計算 - 我的第一篇我不能發佈圖像所以看位於公式:

爲了做到這一點,你可以讓ķ有變動,以及對每個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個combinaisons,分佈如下:1個元件(K = 1)12元件的

  • 12個combinaisons的

    • 4 combinaisons(K = 2)24元件(k的
    • 24個combinaisons = 3 )
    • 24元件(K = 4)的
    • 24 combinaisons

    你可以看到,我的程序是對於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

  • +0

    第二個等式我無法張貼becasue作爲一個新用戶我沒有「聲譽」islocated在:http://i.stack.imgur.com/my6wv.gif – Fafhrd 2013-02-15 09:31:21

    3

    你問的是不平凡的。

    讓我們看兩個字符串,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

    雖然這個想法是相似的,但OP在文本中要求*組合*,並且儘管在方法名稱中使用了「置換」一詞。 – 2013-02-15 01:44:03

    +1

    我不同意。在他的輸出示例中,CA是一個排列組合。 – 2013-02-15 01:46:56

    +0

    這取決於你是否將它解釋爲一個集合或一個列表。由於AC沒有得到,我把它作爲一個集合。 – 2013-02-15 02:36:52

    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 
    
    +0

    一個地圖似乎混淆了這個問題背後的組合思想...... – 2013-02-15 02:39:46

    +0

    就像我說的,從上面的問題中不清楚。 「他的意思是」「其他任何組合」?如果問題明確,組合的想法將會清晰。然後,這完全是關於邏輯。 – Prince 2013-02-15 03:18:56

    +0

    儘管問題不明,但我沒有看到每個String和URL之間的明確關係,所以Map是不合適的。 – 2013-02-15 03:24:36

    1

    短版任意設定大小的工作,與仿製藥,使用guava,以及排列方法given here

    基本思路如下:

    1. 生成冪,丟棄空集
    2. 對於每一組的冪的,生成所有排列

      公共類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]] 
    
    相關問題