和谐敏感词
🔗 题目地址
🎉 模拟
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
String s = scanner.next();
String[] words = new String[n];
for (int i = 0; i < n; i++) {
words[i] = scanner.next();
}
boolean[] st = new boolean[s.length()];
for (int i = 0; i < n; i++) {
int pos = 0;
while(true) {
pos = s.indexOf(words[i], pos);
if(pos == -1)
break;
for (int j = pos; j < pos + words[i].length(); j++)
st[j] = true;
pos += 1;
}
}
char[] ans = s.toCharArray();
for(int i = 0; i < st.length; i++)
if(st[i])
ans[i] = '*';
System.out.println(ans);
}
}
每个区间内第k小的数
🔗 题目地址
🎉 大根堆
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
PriorityQueue<Integer> heap = new PriorityQueue<>((o1, o2) -> o2 - o1);
for (int i = 0; i < n; i++) {
if (i < k - 1) {
heap.add(a[i]);
System.out.print(-1 + " ");
continue;
}
if(heap.size() < k)
heap.add(a[i]);
else{
if(a[i] < heap.peek()){
heap.poll();
heap.add(a[i]);
}
}
System.out.print(heap.peek() + " ");
}
}
}
挑选战队
🔗 题目地址
🎉 二分 + 状态压缩DP
import java.util.Scanner;
public class Main {
private static final int INT_MAX = Integer.MAX_VALUE;
static int n,m,k;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt(); // 读取样例个数
for (int testCase = 0; testCase < t; testCase++) {
n = sc.nextInt(); // 读取n
m = sc.nextInt(); // 读取m
k = sc.nextInt(); // 读取k
int[][] attr = new int[n][m]; // 创建属性数组
// 读取每个队员的属性值
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
attr[i][j] = sc.nextInt();
}
}
// 二分查找初始化
int l = 1, r = 1005, ans = 0;
while (l < r) {
int mid = (l + r) >> 1; // 计算中间值
// 检查是否可以通过选择k个人达到战斗力mid
if (isPossible(attr, mid)) {
ans = mid; // 更新答案
l = mid + 1; // 增大左边界
} else {
r = mid; // 缩小右边界
}
}
System.out.println(ans); // 输出结果
}
sc.close();
}
/**
* 检查是否可以通过选择k个人达到战斗力mid
* @param attr 队员的属性数组
* @param mid 当前尝试的战斗力
* @return 是否可以达到战斗力mid
*/
private static boolean isPossible(int[][] attr, int mid) {
int fullMask = (1 << m) - 1; // 全属性掩码
int[] masks = new int[n]; // 每个队员的属性掩码
for (int i = 0; i < n; i++) { // 枚举每个队员
int mask = 0;
for (int j = 0; j < m; j++) {// 枚举队员的每个属性
if (attr[i][j] >= mid) {
mask |= (1 << j); // 更新掩码
}
}
masks[i] = mask; // 存储掩码
}
int[] dp = new int[1 << m]; // dp[state]:表示达到 state 的状态需要的最少人数
for (int i = 0; i < dp.length; i++) {
dp[i] = INT_MAX; // 初始化dp数组
}
dp[0] = 0; // 初始状态
// 更新dp数组
for (int i = 0; i < n; i++) {
int pmask = masks[i]; // 当前队员的属性掩码
for (int state = 0; state < fullMask; state++) {
if (dp[state] != INT_MAX) { // 起点是 state,借助 pmask 转移,重点是(state|pmask)
dp[state | pmask] = Math.min(dp[state | pmask], dp[state] + 1); // 更新dp数组
}
}
// for (int state = fullMask; state >= 0; state--) {
// if (dp[state] != INT_MAX) { // 起点是 state,借助 pmask 转移,重点是(state|pmask)
// dp[state | pmask] = Math.min(dp[state | pmask], dp[state] + 1); // 更新dp数组
// }
// }
}
return dp[fullMask] <= k; // 检查是否可以通过选择k个人达到战斗力mid
}
}