字符串集合求并集

来源:互联网 发布:淘宝刷直通车是黑车 编辑:程序博客网 时间:2024/04/29 04:56

輸入:

{"aaa","bbb","ccc"}

{"bbb","ddd"}

{"ddd",hhh"}

{"xx","yy"}

{"yy","zz"}

{"ggg"}


輸出:

{"aaa","bbb","ccc","ddd","hhh"}

{"xx","yy","zz"}

{"ggg"}


思路

1. unionset  "ddd"的parent "bbb", "bbb"的parent "aaa", 遞歸查找。注意單個元素 "ggg"的 parent"ggg"

2. hash_map 初始化一個值0,每加一個字符串遞增: "aaa"->0, "bbb"->1.同時,相互映射: 1->"aaa", 2->"bbb"

3. 設一個A數組,長度為字符串單獨的個數。"aaa","bbb","ddd","aaa"則為3。為了方便這裡就取100了

4. union_set: 循環所有字符集合,進行unionset。"aaa"->"bbb", "aaa"->"ccc",具體的unionset方法見代碼。

5. 循環A數組,如果下標元素的值 < -1,說明有字符串,從hash_map中取下標獲得字符串。

6. 內層循環A數組,如果內層下標元的值 = 外層下標,從hash_map中取內層下標獲得字符串。


String[] str0 = { "aaa", "bbb", "ccc", };String[] str1 = { "bbb", "ddd", };String[] str2 = { "eee", "fff", };String[] str3 = { "ggg", };String[] str4 = { "ddd", "hhh", };String[] str5 = { "xx", "yy", };String[] str6 = { "zz", "yy", };String[][] strs = { str0, str1, str2, str3, str4, str5, str6 };

for (int i = 0; i < strs.length; i++) {for (int j = 0; j < strs[i].length; j++) {String key = strs[i][j];if (!map.containsKey(key)) {map.put(index,key);map.put(key, index);index++;}}}System.out.println(map);
此時,map的內容:
{hhh=7, 0=aaa, 1=bbb, 2=ccc, aaa=0, 3=ddd, ddd=3, 4=eee, 5=fff, 6=ggg, 7=hhh, 8=xx, 9=yy, zz=10, 10=zz, yy=9, xx=8, bbb=1, eee=4, fff=5, ggg=6, ccc=2}


for (int i = 0; i < strs.length; i++) {for (int j = 1; j < strs[i].length; j++) {int root1 = find_root(A,(int)map.get(strs[i][0]));int root2 = find_root(A,(int)map.get(strs[i][j]));union(A, root1, root2);}if (strs[i].length == 1) {int root1 = find_root(A,(int)map.get(strs[i][0]));int root2 = find_root(A,(int)map.get(strs[i][0]));union(A, root1, root2);}}

數組的結果:

[-2, 0, 0, 0, -2, 4, -2, 0, -2, 8, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]


所有代碼:

import java.util.*;class DisjointSetProblem {public static void main(String[] args) {int[] A = new int[100];for (int i = 0; i < A.length; i++) {A[i] = -1;}Map<Object, Object> map = new HashMap<>();List<List<String>> result = new ArrayList<>();int index = 0;String[] str0 = { "aaa", "bbb", "ccc", };String[] str1 = { "bbb", "ddd", };String[] str2 = { "eee", "fff", };String[] str3 = { "ggg", };String[] str4 = { "ddd", "hhh", };String[] str5 = { "xx", "yy", };String[] str6 = { "zz", "yy", };String[][] strs = { str0, str1, str2, str3, str4, str5, str6 };for (int i = 0; i < strs.length; i++) {for (int j = 0; j < strs[i].length; j++) {String key = strs[i][j];if (!map.containsKey(key)) {map.put(index,key);map.put(key, index);index++;}}}System.out.println(map);for (int i = 0; i < strs.length; i++) {for (int j = 1; j < strs[i].length; j++) {int root1 = find_root(A,(int)map.get(strs[i][0]));int root2 = find_root(A,(int)map.get(strs[i][j]));union(A, root1, root2);}if (strs[i].length == 1) {int root1 = find_root(A,(int)map.get(strs[i][0]));int root2 = find_root(A,(int)map.get(strs[i][0]));union(A, root1, root2);}}for (int i = 0; i < 26; i++) {List<String> slst = new ArrayList<>();if (A[i] < -1) {String sb = (String)map.get(i);slst.add(sb);}for (int j = 0; j < 26; j++) {if (A[j] == i) {String sb = (String)map.get(j);slst.add(sb);}}if (slst.size() > 0) {result.add(slst);}}for (List<String> l : result) {System.out.println(l);}}public static void union(int[] arr, int root1, int root2) {if (arr[root1] < arr[root2]) {arr[root2] = root1;} else if (arr[root1] > arr[root2]) {arr[root1] = root2;} else {arr[root1]--;if (root1 != root2)arr[root2] = root1;}}public static int find_root(int[] arr, int x) {if (arr[x] <= -1)return x;else {return find_root(arr, arr[x]);}}}


0 0
原创粉丝点击