字符串集合求并集
来源:互联网 发布:淘宝刷直通车是黑车 编辑:程序博客网 时间: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]);}}}
- 字符串集合求并集
- 字符串集合求并集和交集
- 求集合并集。
- 求集合并集
- 求集合并集
- 求集合并集
- 求集合并集
- 求集合 并集
- 数据结构:求集合并集
- java字符串求并集
- java求集合交集和并集
- 用链表实现两个集合求并集
- 集合存取对象、清除、求并集
- 线性表【项目 - 求集合并集】
- 第三周--求集合并集
- 第三周 求并集合集
- 第三周-求集合并集
- 第三周--【项目 - 求集合并集】
- 超级详细的Tarjan算法
- 重载与覆盖的区别
- 来自为知笔记
- 自己办的网线,使用路由器连不上网怎办?
- 零基础unity3d游戏开发系列目录
- 字符串集合求并集
- android 监听wifi连接
- git学习总结(2)基本操作
- TCP/IP详解卷1 读书笔记:第二十六章 Telnet协议
- POJ-3158
- ios内存管理&1之常见的内存管理
- poj2406 Power Strings
- STM32标准库文件使用
- PHP Ajax 跨域问题最佳解决方案