猿辅导笔试

单选题×15, 程序题×3

单选有程序输出题、数据库的题、管道的题、http3.0、概率题等等

程序题第一题

第一行输入组数
第二行第一个输出字符串的数量N,后面的接N个字符串,使用空格隔开
第三行是需要删除的字符串,?表示任意一个字符
求出现最多的字符串的个数
输入:
2
12 A tidy tiger tied a tie tighter to tidy her tiny tail
1 a
16 A big black bug bit a big black bear made the big black bear bleed blood
2 b?? b???

输出:

2(tidy有2个)

3(black有3个)

代码:

import java.util.HashMap;
import java.util.Scanner;

public class Main{

    /*
    * 2
12 A tidy tiger tied a tie tighter to tidy her tiny tail
1 a
16 A big black bug bit a big black bear made the big black bear bleed blood
2 b?? b???
    *
    * */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int M = sc.nextInt();
        sc.nextLine();
        for(int i=0;i<M;i++){
            String[] input = sc.nextLine().toLowerCase().split(" ");
            int N = Integer.parseInt(input[0]);
            String[] stop_word = sc.nextLine().split(" ");
            int K = Integer.parseInt(stop_word[0]);
            HashMap<String, Integer> map = new HashMap<>();
            for(int j=1;j<input.length;j++){
                int t;
                for(t=1;t<stop_word.length;t++){
                    if(compare(input[j], stop_word[t])){
                       break;
                    }
                }
                if(t == stop_word.length){
                    map.put(input[j], map.getOrDefault(input[j].toLowerCase(), 0) + 1);
                }
            }
            int res = 0;
            for(String s : map.keySet()){
                System.out.println(s);
                int tmp = map.get(s);
                res = res > tmp ? res : tmp;
            }
            System.out.println(res);
        }
    }

    public static boolean compare(String s, String t){
        if(s.length() != t.length()){
            return false;
        }
        s = s.toLowerCase();
        t = t.toLowerCase();
        for(int i=0;i<s.length();i++){
            if(s.charAt(i) == t.charAt(i) || t.charAt(i) == '?'){
                continue;
            }else{
                return false;
            }
        }
        return true;
    }
}

第二题:求包含所有质因数的最小子数组的长度

第一行为N
第二行第一个数为K,第二个数为N,代表数组的大小
第三行是数组
输入:
2
20 8
1 2 3 2 6 5 2 1
17 10
1 4 5 7 10 8 7 17 2 8

输出:4(20的所有质因数包括2,2,5, 其中最小的子数组包含2,6,5,2)

这题是最小覆盖字串的变体,我是在考完才写出来,嘤嘤嘤

import java.util.*;
public class Test2 {

    /*
    * 2
20 8
1 2 3 2 6 5 2 1
17 10
1 4 5 7 10 8 7 17 2 8
    * */

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int M = Integer.parseInt(sc.nextLine());
        for(int i=0;i<M;i++){
            String[] input = sc.nextLine().split(" ");
            int K = Integer.parseInt(input[0]);
            int N = Integer.parseInt(input[1]);
            int[] A = new int[N];
            String[] s = sc.nextLine().split(" ");
            for(int j=0;j<N;j++){
                A[j] = Integer.parseInt(s[j]);
            }
            map = new HashMap<>();
            zhiyinzi(K, 2);
            print();
            int left = 0, right = 0;
            int valid = 0;
            int start = 0, end = N+1;
            Map<Integer, Integer> window = new HashMap<>();
            while(right < N){
                int tmp = A[right];
                right++;
                if(map.containsKey(tmp)){
                    window.put(tmp, window.getOrDefault(tmp, 0) + 1);
                    if(window.get(tmp).equals(map.get(tmp))){
                        valid++;
                    }
                }
                while(valid == map.size()){
                    if(right - left < end - start){
                        start = left;
                        end = right;
                    }
                    int left_delete = A[left];
                    left++;
                    if(map.containsKey(left_delete)){
                        if(window.get(left_delete).equals(map.get(left_delete))){
                            valid--;
                        }
                        window.put(left_delete, window.getOrDefault(left_delete, 0) - 1);
                    }
                }
            }
            if(end == N + 1){
                System.out.println(0);
            }else{
                System.out.println(end - start);
            }
        }
    }

    public static void print(){
        for(int key : map.keySet()){
            System.out.println("key: " + key + " " + "value:" + map.get(key));
        }
    }

    public static boolean iszhishu(int m){
        for(int i = 2;i<m;i++){
            if(m % i == 0){
                return false;
            }
        }
        return true;
    }

    static HashMap<Integer, Integer> map;

    public static void zhiyinzi(int n, int m){
        if(n < m){
            return;
        }else{
            if(iszhishu(m) && n % m == 0){
                map.put(m, map.getOrDefault(m, 0) + 1);
                zhiyinzi(n/m, m);
            }else{
                zhiyinzi(n, m+1);
            }
        }
    }
}

 第三题没看,没时间了,一个半小时时间太赶了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值