【垃圾短信识别】【2023 Q1 | 100分】
import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
class Main {
public static void main(String[] args) {
// 处理输入
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] id_pairs = new int[n][2];
for (int i = 0; i < n; i++) {
id_pairs[i][0] = in.nextInt();
id_pairs[i][1] = in.nextInt();
}
int id = in.nextInt();
//发送短信和收到短信的统计信息
LinkedList<Integer> send_list = new LinkedList<>();
LinkedList<Integer> receive_list = new LinkedList<>();
// key为指定ID, value为其send的个数
HashMap<Integer, Integer> send_map = new HashMap<>();
// key为指定ID, value为其receive 的个数
HashMap<Integer, Integer> receive_map = new HashMap<>();
for (int[] id_pair : id_pairs) {
int sender = id_pair[0];
int receiver = id_pair[1];
if (sender == id) {
send_list.addLast(receiver);
send_map.put(receiver, send_map.getOrDefault(receiver, 0) + 1);
}
if (receiver == id) {
receive_list.addLast(sender);
receive_map.put(sender, receive_map.getOrDefault(sender, 0) + 1);
}
}
// 去重结果
HashSet<Integer> send_set = new HashSet<>(send_list);
HashSet<Integer> receive_set = new HashSet<>(receive_list);
// 交集
List<Integer> intersect = send_list.stream().filter(receive_set::contains).collect(Collectors.toList());
// 两个指标 L & M
int L = send_set.size() - intersect.size();
int M = send_list.size() - receive_list.size();
boolean flag = false;
if (L > 10 || M > 5) {
flag = true;
}
if (!flag) {
// 指标 N
for (Integer single_id : intersect) {
if (send_map.containsKey(single_id)
&& receive_map.containsKey(single_id)
&& send_map.get(single_id) - receive_map.get(single_id) > 5) {
flag = true;
break;
}
}
}
System.out.println(flag + " " + L + " " + M);
}
}
【机房布局】【2023 Q1 | 100分】
import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
import java.math.BigInteger;
import java.util.stream.Stream;
class Main {
public static void main(String[] args) {
// 处理输入
Scanner in = new Scanner(System.in);
String input_str = in.nextLine();
int n = input_str.length();
LinkedList<Integer> stack = new LinkedList<>();
boolean flag = false;
for (int i = 0; i < n; i++) {
if (input_str.charAt(i) == 'M') {
// 无效判断
// 1:中间无效
if (i - 1 >= 0 && input_str.charAt(i - 1) == 'M' && i + 1 < n && input_str.charAt(i + 1) == 'M') {
System.out.println(-1);
return;
}
// 2:两边无效
if ((i - 1 == 0 && input_str.charAt(i - 1) == 'M') || (i+1==n && input_str.charAt(i - 1) == 'M')) {
System.out.println(-1);
return;
}
// 当前机柜的左右位置
int pos_left = Math.max(0, i - 1);
int pos_right = Math.min(n - 1, i + 1);
if (stack.size() > 0 && !flag) {
// 满足 MIM 的样式
if (stack.getLast() == pos_left) {
stack.removeLast();
flag = true;
}
} else {
flag = false;
}
stack.addLast(pos_right);
}
}
System.out.println(stack.size());
}
}
【缓存需要最少金币数 /静态扫描】
import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
import java.math.BigInteger;
import java.util.stream.Stream;
class Main {
public static void main(String[] args) {
// 处理输入
Scanner in = new Scanner(System.in);
int m = Integer.parseInt(in.nextLine());
List<Integer> file_ids =Arrays.stream(in.nextLine().split(" "))
.map(Integer::parseInt)
.collect(Collectors.toList());
List<Integer> sizes =Arrays.stream(in.nextLine().split(" "))
.map(Integer::parseInt)
.collect(Collectors.toList());
// key为文件标识 value为文件出现的次数
HashMap<Integer, Integer> file_map = new HashMap<>();
// key为文件标识 value为扫描成本
HashMap<Integer, Integer> file_cost = new HashMap<>();
for (int i = 0; i < file_ids.size(); i++) {
file_map.put(file_ids.get(i), file_map.getOrDefault(file_ids.get(i), 0) + 1);
file_cost.putIfAbsent(file_ids.get(i), sizes.get(i));
}
int result = 0;
for (int k : file_map.keySet()) {
result += Math.min(file_map.get(k) * file_cost.get(k), file_cost.get(k) + m);
}
System.out.println(result);
}
}
【快递投放问题】【2023 Q1 | 100分
import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
import java.math.BigInteger;
import java.util.stream.Stream;
class Main {
public static void main(String[] args) {
// 处理输入
Scanner in = new Scanner(System.in);
int m = in.nextInt();
int n = in.nextInt();
in.nextLine();
//包裹信息
String[][] package_info = new String[m][];
for (int i = 0; i < m; i++) {
package_info[i] = in.nextLine().split(" ");
}
//检查站信息
String[][] checkpoint = new String[n][];
for (int i = 0; i < n; i++) {
checkpoint[i] = in.nextLine().split(" ");
}
HashMap<String, HashSet<String>> package_map = new HashMap<>();
HashMap<String, HashSet<String>> checkpoint_map = new HashMap<>();
for (String[] single_package : package_info) {
//合并起点终点作为key
package_map.putIfAbsent(single_package[1] + "-" + single_package[2], new HashSet<>());
package_map.get(single_package[1] + "-" + single_package[2]).add(single_package[0]);
}
for (String[] single_checkpoint : checkpoint) {
checkpoint_map.putIfAbsent(single_checkpoint[0] + "-" + single_checkpoint[1], new HashSet<>());
checkpoint_map.get(single_checkpoint[0] + "-" + single_checkpoint[1]).addAll(Arrays.asList(Arrays.copyOfRange(single_checkpoint, 2, single_checkpoint.length)));
}
ArrayList<String> result = new ArrayList<>();
for (String key : package_map.keySet()) {
HashSet<String> packages = package_map.get(key);
HashSet<String> banned_package = checkpoint_map.get(key);
if (banned_package == null) {
continue;
}
for (String single_package : packages) {
if (banned_package.contains(single_package)) {
result.add(single_package);
}
}
}
if (result.size() == 0) {
System.out.println("none");
} else {
//按包裹名排序
result.sort((a, b) -> Integer.parseInt(a.substring(7)) - Integer.parseInt(b.substring(7)));
for (int i=0;i<result.size();i++) {
System.out.println(result.get(i));
if (i!=result.size()-1) {
System.out.println(" ");
}
}
}
}
}
【优秀学员统计】【2022.11 Q4 新题】
import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
import java.math.BigInteger;
import java.util.stream.Stream;
class Main {
public static void main(String[] args) {
// 处理输入
Scanner in = new Scanner(System.in);
//新员工数量
int n = in.nextInt();
// 每天打卡的员工数量
int[] employee_count = new int[30];
for (int i = 0; i < 30; i++) {
employee_count[i] = in.nextInt();
}
// 打卡记录
int[][] employee_ids = new int[30][];
for (int i = 0; i < 30; i++) {
employee_ids[i] = new int[employee_count[i]];
for (int j = 0; j < employee_count[i]; j++) {
employee_ids[i][j] = in.nextInt();
}
}
//key为员工ID, value为其打卡的记录信息:[打卡次数,首次打卡index]
HashMap<Integer, Integer[]> employee_info = new HashMap<>();
for (int i = 0; i < employee_ids.length; i++) {
for (int id : employee_ids[i]) {
if (employee_info.containsKey(id)) {
employee_info.get(id)[0]++;
} else {
employee_info.put(id, new Integer[] {1, i});
}
}
}
// 将map信息转到list中,以便后续排序
ArrayList<Integer[]> employee_list = new ArrayList<>();
for (Integer id : employee_info.keySet()) {
employee_list.add(new Integer[] {id, employee_info.get(id)[0], employee_info.get(id)[1]});
}
employee_list.sort((a, b) ->
a[1].equals(b[1]) ? (a[2].equals(b[2]) ? a[0] - b[0] : a[2] - b[2]) : b[1] - a[1]);
//输出
for (int i = 0; i < 5; i++) {
System.out.print(employee_list.get(i)[0]);
if (i!=4) {
System.out.print(" ");
}
}
}
}
【求小火车最多人时所在园区站点】【人数最多的站点】【100%通过率】【2022 Q4】
import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
import java.math.BigInteger;
import java.util.stream.Stream;
class Main {
public static void main(String[] args) {
// 处理输入
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] nums = new int[n][2];
int max_site = 0;
for (int i = 0; i < n; i++) {
int start = in.nextInt();
int end = in.nextInt();
max_site = Math.max(max_site, Math.max(start,end));
nums[i] = new int[]{start, end};
}
ArrayList<Integer[]> sites = new ArrayList<>();
for (int i=0;i<nums.length;i++) {
if (nums[i][0] > nums[i][1]) {
sites.add( new Integer[]{nums[i][0], max_site});
sites.add( new Integer[]{1, nums[i][1]});
} else {
sites.add( new Integer[]{nums[i][0], nums[i][1]});
}
}
//创建人数的数据结构
HashMap<Integer, Integer> site_map = new HashMap<>();
for (Integer[] pair : sites) {
for (int i = pair[0]; i <= pair[1]; i++) {
site_map.put(i, site_map.getOrDefault(i, 0) + 1);
}
}
LinkedList<Map.Entry<Integer, Integer>> site_count = new LinkedList<>(site_map.entrySet());
//先按照人数排序,人数
site_count.sort((e1, e2) -> Integer.compare(e2.getValue(), e1.getValue()));
site_count.sort(new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
int res = o2.getValue() - o1.getValue();
if (res == 0) {
res = o1.getKey() - o2.getKey();
}
return res;
}
});
System.out.println(site_count.get(0).getKey());
}
}
【寻找相似单词】【2023 Q1 | 100分】
import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
import java.math.BigInteger;
import java.util.stream.Stream;
class Main {
public static void main(String[] args) {
// 处理输入
Scanner in = new Scanner(System.in);
int N = Integer.parseInt(in.nextLine());
String[] words = new String[N];
for(int i=0; i<N; i++){
words[i] = in.nextLine();
}
String target_word = in.nextLine();
List<String> result = new ArrayList<String>();
for (int i=0; i<N; i++){
// 比较两个字符串
if(words[i].length() == target_word.length() && check(target_word, words[i])){
result.add(words[i]);
}
}
//排序
Collections.sort(result);
if (result.size() == 0) {
System.out.println("null");
} else {
for (int i=0;i<result.size();i++) {
System.out.print(result.get(i));
if (i !=result.size()-1 ) {
System.out.print(" ");
}
}
}
}
public static boolean check(String a, String b){
char[] a_arr = a.toCharArray();
char[] b_arr = b.toCharArray();
Arrays.sort(a_arr);
Arrays.sort(b_arr);
for(int i=0; i<a_arr.length; i++){
if(a_arr[i] != b_arr[i]){
return false;
}
}
return true;
}
}
【严格递增字符串】【2023 Q1 | 100分】
import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
import java.math.BigInteger;
import java.util.stream.Stream;
class Main {
public static void main(String[] args) {
// 处理输入
Scanner in = new Scanner(System.in);
String input_str = in.nextLine();
System.out.println(minFlipsMonoIncr(input_str));
}
public static int minFlipsMonoIncr(String s) {
int n = s.length();
int dp0 = 0, dp1 = 0;
for (int i = 0; i < n; i++) {
char c = s.charAt(i);
int dp0New = dp0, dp1New = Math.min(dp0, dp1);
if (c == 'B') {
dp0New++;
} else {
dp1New++;
}
dp0 = dp0New;
dp1 = dp1New;
}
return Math.min(dp0, dp1);
}
}
【微服务的集成测试】【2023 Q1 | 100分】
import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
import java.math.BigInteger;
import java.util.stream.Stream;
class Main {
public static void main(String[] args) {
// 处理输入
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
Integer[][] service_map = new Integer[n][n];
for (int i = 0; i < n; i++) {
service_map[i] = Arrays.stream(in.nextLine().split(" ")).map(Integer::parseInt).toArray(Integer[]::new);
}
int target = in.nextInt() - 1;
// 每个任务的前置依赖任务
List<List<Integer>> upstream =new ArrayList<List<Integer>>(n);
for (int i=0;i<n;i++) {
upstream.add(new ArrayList<>());
}
//初始化前置依赖任务
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j && service_map[i][j] == 1) {
upstream.get(i).add(j);
}
}
}
// 保存服务列表
LinkedList<List<Integer>> service_list =new LinkedList<List<Integer>>();
service_list.add(upstream.get(target));
//等待时间初始化
int result = service_map[target][target];
while (service_list.size() > 0) {
List<Integer> upstream_tasks = service_list.removeFirst();
int temp_res = 0;
ArrayList<Integer> services = new ArrayList<>();
for (int i=0;i<upstream_tasks.size();i++) {
temp_res = Math.max(temp_res, service_map[i][i]);
for (int j=0;j<upstream.get(upstream_tasks.get(i)).size();j++) {
services.add(upstream.get(upstream_tasks.get(i)).get(j));
}
}
result += temp_res;
if (services.size() > 0) {
service_list.add(services);
}
}
System.out.println(result);
}
}
【组合出合法最小数】【2023 Q1 | 100分】
import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
import java.math.BigInteger;
import java.util.stream.Stream;
class Main {
public static List<String> result = new ArrayList<>();
public static void main(String[] args) {
// 处理输入
Scanner in = new Scanner(System.in);
String[] numbers = in.nextLine().split(" ");
//第一步首先要对数字进行排序
//直接对string进行排序即可
Arrays.sort(numbers);
// 当前数字是否使用过
boolean[] used = new boolean[numbers.length];
backfill(numbers, used, new ArrayList<>());
ArrayList<String> not_start_with_0_res = new ArrayList<>();
for (int i=0;i<result.size();i++) {
if (result.get(i).charAt(0) != '0') {
not_start_with_0_res.add(result.get(i));
}
}
// 注意全程不能转成数字,不然会溢出
System.out.println( not_start_with_0_res.size() > 0 ? not_start_with_0_res.get(0) : result.get(0).replaceFirst("^0*", ""));
}
// 回溯全排列
public static void backfill(String[] numbers, boolean[] used, ArrayList<String> path) {
if (path.size() == numbers.length) {
result.add(path.stream().collect(Collectors.joining()));
}
for (int i = 0; i < numbers.length; i++) {
if (used[i]) {
continue;
}
path.add(numbers[i]);
used[i] = true;
backfill(numbers, used, path);
for (int i=0;i<result.size();i++) {
if (result.get(i).charAt(0) != '0') {
return;
}
}
used[i] = false;
path.remove(path.size() - 1);
}
}
}