-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwk324.java
147 lines (131 loc) · 4.16 KB
/
wk324.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package weekly;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class wk324 {
//ranking: 1206 / 4167
//暴力
/* public int similarPairs(String[] words) {
List<int[]> list = new ArrayList<>();
for (int i = 0; i < words.length; i++) {
int[] count = new int[26];
for (char c : words[i].toCharArray()) {
count[c - 'a']++;
}
list.add(count);
}
int ans = 0;
for (int i = 0; i < words.length; i++) {
int[] count = list.get(i);
for (int j = i + 1; j < words.length; j++) {
boolean flag = true;
int[] count2 = list.get(j);
for (int k = 0; k < count.length; k++) {
if ((count[k] == 0 && count2[k] == 0) || (count[k] > 0 && count2[k] > 0)) {
continue;
} else {
flag = false;
break;
}
}
if (flag) {
System.out.println(i + " " + j);
ans++;
}
}
}
return ans;
}*/
//位运算
public int similarPairs(String[] words) {
int ans=0;
Map<Integer,Integer> map=new HashMap<>();
for (int i = 0; i < words.length; i++) {
int mask=0;
for (char c : words[i].toCharArray()) {
mask|=(1<<(c-'a'));
}
ans+=map.getOrDefault(mask,0);
map.put(mask,map.getOrDefault(mask,0)+1);
}
return ans;
}
public int smallestValue(int n) {
int sum = 0;
for (int i = 2, m = n; m > 1; i++) {
while (m % i == 0) {
m /= i;
sum += i;
}
}
return sum == n ? n : smallestValue(sum);
}
public boolean isPossible(int n, List<List<Integer>> edges) {
Map<Integer, Set<Integer>> map = new HashMap<>();
for (List<Integer> edge : edges) {
int a = edge.get(0);
int b = edge.get(1);
if (!map.containsKey(a)) map.put(a, new HashSet<>());
if (!map.containsKey(b)) map.put(b, new HashSet<>());
map.get(a).add(b);
map.get(b).add(a);
}
List<Integer> list = new ArrayList<>();
for (Map.Entry<Integer, Set<Integer>> entry : map.entrySet()) {
if (entry.getValue().size() % 2 != 0) {
//奇数的点
list.add(entry.getKey());
}
}
if (list.size() > 4 || list.size() % 2 != 0) {
return false;
}
if (list.size() == 0) return true;
if (list.size() == 2) {
int a = list.get(0);
int b = list.get(1);
for (Map.Entry<Integer, Set<Integer>> entry : map.entrySet()) {
if (!entry.getValue().contains(a) && !entry.getValue().contains(b)) {
return true;
}
}
return false;
} else {
int a = list.get(0);
int b = list.get(1);
int c = list.get(2);
int d = list.get(3);
return (check(a, b, map) && check(c, d, map)) || (check(a, c, map) && check(b, d, map)) || (check(a, d, map) && check(b, c, map));
}
}
boolean check(int a, int b, Map<Integer, Set<Integer>> map) {
if (!map.get(a).contains(b)) {
return true;
}
return false;
}
public int[] cycleLengthQueries(int n, int[][] queries) {
int[] res = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
int a = queries[i][0];
int b = queries[i][1];
res[i] = findFather(a, b);
}
return res;
}
public double log2(double N) {
return Math.log(N) / Math.log(2);
}
int findFather(int i, int j) {
int res=0;
while (i != j) {
res++;
if (i > j) i /= 2;
else j /= 2;
}
return res;
}
}