猿辅导8.26笔试
第一题,找出没有匹配上停用词的单词的最高频数,直接暴力
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int M = Integer.parseInt(br.readLine().trim()); while(M -- > 0){ String[] words = br.readLine().trim().split(" "); int N = Integer.parseInt(words[0]); String[] regs = br.readLine().trim().split(" "); int K = Integer.parseInt(regs[0]); Map<String, Integer> map = new HashMap<>(); a : for (int i = 1; i <= N; i++) { String str = words[i].toLowerCase(); for (int j = 1; j <= K; j++) { if(fit(str, regs[j].toLowerCase())){ continue a; } } map.put(str, map.getOrDefault(str, 0) + 1); } int max = 0; for(String str : map.keySet()){ max = Math.max(map.get(str), max); } System.out.println(max); } } private static boolean fit(String str, String reg){ if(str.length() != reg.length()){ return false; } for(int i = 0; i < str.length(); i++){ char c = reg.charAt(i); if(c != '?' && str.charAt(i) != c){ return false; } } return true; } }第二题:数字K分解质因数,且都包含在数组中最小数组子集长度,暴力过
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int M = Integer.parseInt(br.readLine().trim()); while(M-- > 0){ String[] params = br.readLine().trim().split(" "); int K = Integer.parseInt(params[0]); int N = Integer.parseInt(params[1]); int[] arr = new int[N]; boolean[] isPrime = new boolean[N]; params = br.readLine().trim().split(" "); int minLen = Integer.MAX_VALUE; for (int i = 0; i < N; i++) { arr[i] = Integer.parseInt(params[i]); isPrime[i] = isPrime(arr[i]); } for(int i = 0; i < N; i++){ if(!isPrime[i] || K % arr[i] != 0){ continue; } int temp = K / arr[i]; if(temp == 1){ minLen = 1; break; } for(int j = i + 1; j < N; j++){ if(!isPrime[j] || temp % arr[j] != 0){ continue; } temp /= arr[j]; if(temp == 1){ minLen = Math.min(minLen, j - i + 1); break; } } } System.out.println(minLen); } } private static boolean isPrime(int n){ if(n == 1){ return false; } for(int i = 2; i < (int)Math.sqrt(n) + 1; i++){ if(n % i == 0){ return false; } } return true; } }第三题:游乐园,不太会