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); } }