并查集的使用
算法概论第六周
题目链接
题目描述
Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that it equals Y.
For example, "tars" and "rats" are similar (swapping at positions 0 and 2), and "rats" and "arts" are similar, but "star" is not similar to "tars", "rats", or "arts".
Together, these form two connected groups by similarity: {"tars", "rats", "arts"} and {"star"}. Notice that "tars" and "arts" are in the same group even though they are not similar. Formally, each group is such that a word is in the group if and only if it is similar to at least one other word in the group.
We are given a list A of strings. Every string in A is an anagram of every other string in A. How many groups are there?
Example 1:
Input: ["tars","rats","arts","star"]
Output: 2
Note:
A.length <= 2000
A[i].length <= 1000
A.length * A[i].length <= 20000
All words in A consist of lowercase letters only.
All words in A have the same length and are anagrams of each other.
The judging time limit has been increased for this question.
思路分析
- 这道题的测试数据经过了升级,为了能够通过,需要根据A.length和A[i].length的值,执行复杂度为 O ( n 2 w ) O(n^2w) O(n2w)和 O ( n w 3 ) O(nw^3) O(nw3)两种算法之一
- O ( n 2 w ) O(n^2w) O(n2w)的思想是使用并查集,首先判断两个字符串是否similiar只需要 O ( w ) O(w) O(w)的复杂度,只需要一个二重循环判断两两字符串是否similiar,如果是就归为同一个集合。
- O ( n w 3 ) O(nw^3) O(nw3)的思想是对于一个字符串,递归枚举出它similiar的所有串,然后用映射的方法来检查符合条件的串
- 比较 n 2 w n^2w n2w和 n w 3 nw^3 nw3的值决定用哪一种算法
代码实现
- 只实现 O ( n 2 w ) O(n^2w) O(n2w)的版本(通过样例58/62)
class Solution {
private:
int ufa[2005] = {};
public:
int numSimilarGroups(vector<string>& A) {
int len = A.size();
// int wlen = A[0].length();
for(int i = 0; i < len; i++){
ufa[i] = i;
}
for(int i = 0; i < len; i++){
for(int j = i + 1; j < len; j++){
if(ufa[i] == ufa[j])
continue;
if(isSimiliar(A[i], A[j])){
merge(i, j);
}
}
}
int count = 0;
for(int i = 0; i < len; i++){
if(ufa[i] == i)
count++;
}
return count;
}
void merge(int a, int b){
int ha = head(a);
int hb = head(b);
if(ha != hb){
ufa[ha] = hb;
}
}
int head(int a){
int pos = a;
while(ufa[pos] != pos){
pos = ufa[pos];
}
return pos;
}
bool isSimiliar(string a, string b){
int len = a.length();
int count = 0;
for(int i = 0; i < len; i++){
if(a[i] != b[i])
count++;
if(count > 2)
return false;
}
if(count == 2 || count == 0)
return true;
return false;
}
};
- 实现 O ( n 2 w ) O(n^2w) O(n2w)和 O ( n w 3 ) O(nw^3) O(nw3)的版本(引用,待研究)
class Solution {
public int numSimilarGroups(String[] A) {
int N = A.length;
int W = A[0].length();
DSU dsu = new DSU(N);
if (N < W*W) { // If few words, then check for pairwise similarity: O(N^2 W)
for (int i = 0; i < N; ++i)
for (int j = i+1; j < N; ++j)
if (similar(A[i], A[j]))
dsu.union(i, j);
} else { // If short words, check all neighbors: O(N W^3)
Map<String, List<Integer>> buckets = new HashMap();
for (int i = 0; i < N; ++i) {
char[] L = A[i].toCharArray();
for (int j0 = 0; j0 < L.length; ++j0)
for (int j1 = j0 + 1; j1 < L.length; ++j1) {
swap(L, j0, j1);
StringBuilder sb = new StringBuilder();
for (char c: L) sb.append(c);
buckets.computeIfAbsent(sb.toString(),
x-> new ArrayList<Integer>()).add(i);
swap(L, j0, j1);
}
}
for (int i1 = 0; i1 < A.length; ++i1)
if (buckets.containsKey(A[i1]))
for (int i2: buckets.get(A[i1]))
dsu.union(i1, i2);
}
int ans = 0;
for (int i = 0; i < N; ++i)
if (dsu.parent[i] == i) ans++;
return ans;
}
public boolean similar(String word1, String word2) {
int diff = 0;
for (int i = 0; i < word1.length(); ++i)
if (word1.charAt(i) != word2.charAt(i))
diff++;
return diff <= 2;
}
public void swap(char[] A, int i, int j) {
char tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
}
class DSU {
int[] parent;
public DSU(int N) {
parent = new int[N];
for (int i = 0; i < N; ++i)
parent[i] = i;
}
public int find(int x) {
if (parent[x] != x) parent[x] = find(parent[x]);
return parent[x];
}
public void union(int x, int y) {
parent[find(x)] = find(y);
}
}