得物笔试复盘

前言

这里是对4月18日的得物笔试的复盘

题目

题目1:

1
2
3
4
5
6
7
8
9
题目描述:
给出一个长度为 n 的仅由大写字母 A 和 B 组成的字符串 S,要求统计有多少个子串(用二元组 <l, r> 表示)恰好包含 k 个 A。

输入格式:
第一行包含两个正整数 n 和 k,表示字符串长度和所需 A 的个数(1 ≤ n, k ≤ 50000)。
第二行是一个由大写字母 A 和 B 组成的字符串 S。

输出格式:
输出一个整数,表示满足条件的子串数量。

解法:前缀和+计数数组:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Main {
public static void main(String[] agrs){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
String s = in.next();
int[] count = new int[n + 1];
int preSum = 0;
int ans = 0;
count[0] = 1;
for(int i = 0; i < n; ++i){
if(s.charAt(i) == 'A'){
preSum++;
}
if(preSum -k >= 0){
ans += count[preSum - k];
}
count[preSum]++;

}
System.out.println(ans);
}

}

注意count[0]是1

题目2:

HashMap<>[] dp

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
import java.util.HashMap;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = scanner.nextInt();
}
scanner.close();

// 边界情况:长度<=2时,所有子序列都是等比子序列
if (n <= 2) {
System.out.println(n);
return;
}

// dp[i]:以第i个元素结尾的,不同公比对应的最长等比子序列长度
HashMap<String, Integer>[] dp = new HashMap[n];
int maxLength = 2; // 最短的等比子序列长度为2

for (int i = 0; i < n; i++) {
dp[i] = new HashMap<>();
for (int j = 0; j < i; j++) {
// 用最简分数表示公比,避免浮点数精度问题
int gcd = gcd(nums[i], nums[j]);
int numerator = nums[i] / gcd;
int denominator = nums[j] / gcd;
String ratio = numerator + "/" + denominator;

// 以j结尾的该公比子序列长度+1(加上当前i元素)
int currentLength = dp[j].getOrDefault(ratio, 1) + 1;
// 更新当前i位置的最长长度
if (currentLength > dp[i].getOrDefault(ratio, 0)) {
dp[i].put(ratio, currentLength);
maxLength = Math.max(maxLength, currentLength);
}
}
}

System.out.println(maxLength);
}

// 计算最大公约数
private static int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}

int currentLength = dp[j].getOrDefault(ratio, 1) + 1;注意这里的默认值设置为1

题目3: 最优缓存替换

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
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int M = in.nextInt();
int K = in.nextInt();
int[] nums = new int[K];
for(int i = 0; i < K; ++i){
nums[i] = in.nextInt();
}
int[] next = new int[K];
Map<Integer, Integer> lastPos = new HashMap<>();
for(int i = K - 1; i>=0; --i){
next[i] = lastPos.getOrDefault(nums[i], K);
lastPos.put(nums[i], i);
}
PriorityQueue<int[]> queue = new PriorityQueue<>((a, b) -> Integer.compare(b[0], a[0]));
int cnt = 0;
HashSet<Integer> cache = new HashSet<>();
for(int i = 0; i < K; ++i){
if(!cache.contains(nums[i])){
cnt++;
cache.add(nums[i]);
if(cache.size() > M){
while(!queue.isEmpty()){
int removed = queue.poll()[1];
if(cache.contains(removed)){
cache.remove(removed);
break;
}else{
queue.poll();
}


}
}
}
queue.offer(new int[]{next[i], nums[i]});

}
System.out.println(cnt);

}

}