|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +public class wkb88 { |
| 7 | + |
| 8 | + //ranking: 354 / 3905 |
| 9 | + |
| 10 | + //简单题,暴力解决 |
| 11 | + public boolean equalFrequency(String word) { |
| 12 | + for (int i = 0; i < word.length(); i++) { |
| 13 | + int[] count = new int[26]; |
| 14 | + for (int j = 0; j < word.toCharArray().length; j++) { |
| 15 | + char c = word.charAt(j); |
| 16 | + if (j == i) continue; |
| 17 | + count[c - 'a']++; |
| 18 | + } |
| 19 | + |
| 20 | + Map<Integer, Integer> map = new HashMap<>(); |
| 21 | + for (int j = 0; j < count.length; j++) { |
| 22 | + if (count[j] != 0) { |
| 23 | + map.put(count[j], map.getOrDefault(count[j], 0) + 1); |
| 24 | + } |
| 25 | + } |
| 26 | + if (map.size() == 1) return true; |
| 27 | + } |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + //用一个指针标记最长前缀 |
| 32 | + class LUPrefix { |
| 33 | + int[] dp; |
| 34 | + int index = 1; |
| 35 | + |
| 36 | + public LUPrefix(int n) { |
| 37 | + dp = new int[n + 1]; |
| 38 | + dp[0] = 1; |
| 39 | + } |
| 40 | + |
| 41 | + public void upload(int video) { |
| 42 | + dp[video] = 1; |
| 43 | + while (index < dp.length && dp[index] == 1) { |
| 44 | + index++; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public int longest() { |
| 49 | + return index - 1; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + //找规律即可 |
| 54 | + public int xorAllNums(int[] nums1, int[] nums2) { |
| 55 | + int ans = 0; |
| 56 | + if (nums2.length % 2 == 1) { |
| 57 | + for (int i : nums1) { |
| 58 | + ans ^= i; |
| 59 | + } |
| 60 | + } |
| 61 | + if (nums1.length % 2 == 1) { |
| 62 | + for (int i : nums2) { |
| 63 | + ans ^= i; |
| 64 | + } |
| 65 | + } |
| 66 | + return ans; |
| 67 | + } |
| 68 | + |
| 69 | + //树状数组板子 |
| 70 | + public class FenwickTree { |
| 71 | + |
| 72 | + /** |
| 73 | + * 预处理数组 |
| 74 | + */ |
| 75 | + private int[] tree; |
| 76 | + private int len; |
| 77 | + |
| 78 | + public FenwickTree(int n) { |
| 79 | + this.len = n; |
| 80 | + tree = new int[n + 1]; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * 单点更新 |
| 85 | + * |
| 86 | + * @param i 原始数组索引 i |
| 87 | + * @param delta 变化值 = 更新以后的值 - 原始值 |
| 88 | + */ |
| 89 | + public void update(int i, int delta) { |
| 90 | + // 从下到上更新,注意,预处理数组,比原始数组的 len 大 1,故 预处理索引的最大值为 len |
| 91 | + while (i <= len) { |
| 92 | + tree[i] += delta; |
| 93 | + i += lowbit(i); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + //区间更新 |
| 98 | + void update(int x, int y, int k) { |
| 99 | + update(x, k); |
| 100 | + update(y + 1, -k); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * 查询前缀和 |
| 105 | + * |
| 106 | + * @param i 前缀的最大索引,即查询区间 [0, i] 的所有元素之和 |
| 107 | + */ |
| 108 | + public int query(int i) { |
| 109 | + // 从右到左查询 |
| 110 | + int sum = 0; |
| 111 | + while (i > 0) { |
| 112 | + sum += tree[i]; |
| 113 | + i -= lowbit(i); |
| 114 | + } |
| 115 | + return sum; |
| 116 | + } |
| 117 | + |
| 118 | + public int lowbit(int x) { |
| 119 | + return x & (-x); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + //把两个数组拼成一个数组,然后线段树查找指定区间值的个数 |
| 124 | + public long numberOfPairs(int[] nums1, int[] nums2, int diff) { |
| 125 | + int MAX = 100000; |
| 126 | + int D = 5 * 10000; |
| 127 | + for (int i = 0; i < nums1.length; i++) { |
| 128 | + nums1[i] = nums1[i] - nums2[i]; |
| 129 | + } |
| 130 | + FenwickTree f = new FenwickTree(MAX); |
| 131 | + long ans = 0; |
| 132 | + for (int i = 0; i < nums1.length; i++) { |
| 133 | + ans += f.query(nums1[i] + diff + D); |
| 134 | + f.update(nums1[i] + D, 1); |
| 135 | + } |
| 136 | + return ans; |
| 137 | + } |
| 138 | + |
| 139 | + //补周赛313第四题 |
| 140 | + public int deleteString(String s) { |
| 141 | + int n = s.length(); |
| 142 | + int[][] lcp = new int[n + 1][n + 1]; |
| 143 | + //求s[i]s[j]的最长公共前缀长度 |
| 144 | + for (int i = n - 1; i >= 0; i--) { |
| 145 | + for (int j = n - 1; j > i; j--) { |
| 146 | + if (s.charAt(i) == s.charAt(j)) { |
| 147 | + lcp[i][j] = lcp[i + 1][j + 1] + 1; |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + int[] dp = new int[n]; |
| 152 | + for (int i = n-1; i >= 0; i--) { |
| 153 | + dp[i]=1; |
| 154 | + for (int j = 1; i + j * 2 <= n; j++) { |
| 155 | + if (lcp[i][i + j] >= j) { |
| 156 | + dp[i]=Math.max(dp[i],dp[i+j]+1); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + return dp[0]; |
| 161 | + } |
| 162 | +} |
0 commit comments