8.19 小红书笔试 AK (笔试已挂)

8月31日更新 流程已终止,未收到面试

1.用一个hash维护当前单词的数量即可

public class Main {
        public static void main(String args[]) {
            Scanner cin = new Scanner(System.in);
            int n = cin.nextInt();
            cin.nextLine();
            HashMap<String, Integer> mp = new HashMap();
            int cnt = 0;
            int cur = 1;
            for (int i = 0; i < n; ++i) {
                String word = cin.nextLine();
                int v = mp.getOrDefault(word, 0) + 1;
                mp.put(word, v);
                if (v == cur) {
                    mp.put(word, Integer.MIN_VALUE);
                    cnt++;
                    cur++;
                }
            }
            System.out.println(cnt);
        }
    }

2.将字符映射成最终的字符后,再判断是否是回文串

public class Main {
        public static void main(String args[]) {
            Scanner cin = new Scanner(System.in);
            int n = cin.nextInt();
            cin.nextLine();
            for (int i = 0; i < n; ++i) {
                boolean flag = true;
                String s = cin.nextLine();
                StringBuilder res = new StringBuilder();
                for (int k = 0; k < s.length(); ++k) {
                    res.append(getFin(s.charAt(k)));
                }
                for (int k = 0, j = res.length() - 1; k < j; ++k, --j) {
                    if (res.charAt(k) != res.charAt(j)) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    System.out.println("YES");
                } else {
                    System.out.println("NO");
                }
            }
        }
  
        public static String getFin(char c) {
            switch (c) {
                case 'w':
                    return "vv";
                case 'm':
                    return "uu";
                case 'b':
                case 'p':
                case 'q':
                    return "d";
                case 'n':
                    return "u";
            }
            return c + "";
        }
    }

3.暴力+剪枝

import java.io.*;
import java.util.*;
public class Main {
        public static void main(String args[]) {
            Scanner cin = new Scanner(System.in);
            int n = cin.nextInt();
            int m = cin.nextInt();
            long k = cin.nextLong();
            int[] a = new int[n];
            long maxm = 0;
            for (int i = 0; i < n; ++i) {
                a[i] = cin.nextInt();
            }
            long[] h = new long[n];
            for (int i = 0; i < n; ++i) {
                h[i] = cin.nextInt();
                //存储单节点的最大值
                if (h[i] <= k) {
                    maxm = Math.max(maxm, a[i]);
                }
            }
            LinkedList<int[]>[] mp = new LinkedList[n];
            for (int i = 0; i < n; ++i) {
                mp[i] = new LinkedList();
            }
            for (int i = 0; i < m; ++i) {
                int u = cin.nextInt() - 1;
                int v = cin.nextInt() - 1;
                int w = cin.nextInt();
                //判断两个节点是否超过k,是的话就不加入,这个不加的话只能过18%
                if (h[u] + h[v] + w > k) continue;
                else if (h[u] + h[v] + w == k) {
                    //判断两个节点是否等于k,并存储最大值
                    maxm = Math.max(maxm, a[u] + a[v]);
                    continue;
                }
                maxm = Math.max(maxm, a[u] + a[v]);
                mp[u].add(new int[]{v, w});
                mp[v].add(new int[]{u, w});
            }
            for (int i = 0; i < n; ++i) {
                for (int[] second : mp[i]) {
                    for (int[] third : mp[second[0]]) {
                        //判断third[0]是否等于i
                        if (third[0] == i) continue;
                        //判断是否超过k
                        if (h[i] + second[1] + h[second[0]] + third[1] + h[third[0]] > k) continue;
                      //不加long 只能过64% 
					  maxm = Math.max(maxm, (long) a[i] + a[second[0]] + a[third[0]]);
                    }
                }
            }
            System.out.println(maxm);
        }
    }

全部评论
第二题为什么直接将所有字符先转换就行,怎么想到的啊,我还在回溯只替换一个和多个的情况
1
送花
回复
分享
发布于 2023-08-19 18:13 湖北
第二题一定要替换吗?
点赞
送花
回复
分享
发布于 2023-08-19 18:18 浙江
滴滴
校招火热招聘中
官网直投
//判断两个节点是否超过k,是的话就不加入,这个不加的话只能过18% 就是卡这了呀。int相加后为负数是吧。害,还得多注意注意!
点赞
送花
回复
分享
发布于 2023-08-19 18:19 湖北
还是浙大牛
点赞
送花
回复
分享
发布于 2023-08-19 18:39 北京
原来暴力能过
点赞
送花
回复
分享
发布于 2023-08-19 20:23 台湾
佬,第二题将m拆成两个n和u对比是哪种情况?相等还是不相等呢
点赞
送花
回复
分享
发布于 2023-08-19 20:42 河南

相关推荐

7 36 评论
分享
牛客网
牛客企业服务