猿辅导笔试

本文解析了一场编程竞赛中的三道题目,包括字符串匹配问题、寻找包含所有质因数的最小子数组问题等。提供了完整的Java代码实现,并对核心算法进行了说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单选题×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);
            }
        }
    }
}

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

### PyCharm 打开文件显示全的解决方案 当遇到PyCharm打开文件显示全的情况时,可以尝试以下几种方法来解决问题。 #### 方法一:清理缓存并重启IDE 有时IDE内部缓存可能导致文件加载异常。通过清除缓存再启动程序能够有效改善此状况。具体操作路径为`File -> Invalidate Caches / Restart...`,之后按照提示完成相应动作即可[^1]。 #### 方法二:调整编辑器字体设置 如果是因为字体原因造成的内容显示问题,则可以通过修改编辑区内的文字样式来进行修复。进入`Settings/Preferences | Editor | Font`选项卡内更改合适的字号大小以及启用抗锯齿功能等参数配置[^2]。 #### 方法三:检查项目结构配置 对于某些特定场景下的源码视图缺失现象,可能是由于当前工作空间未能正确识别全部模块所引起。此时应该核查Project Structure的Content Roots设定项是否涵盖了整个工程根目录;必要时可手动添加遗漏部分,并保存变更生效[^3]。 ```python # 示例代码用于展示如何获取当前项目的根路径,在实际应用中可根据需求调用该函数辅助排查问题 import os def get_project_root(): current_file = os.path.abspath(__file__) project_dir = os.path.dirname(current_file) while not os.path.exists(os.path.join(project_dir, '.idea')): parent_dir = os.path.dirname(project_dir) if parent_dir == project_dir: break project_dir = parent_dir return project_dir print(f"Current Project Root Directory is {get_project_root()}") ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值