• 【小米】二维定长滑动窗口


    返回输入二维字符矩阵中班3*3子矩阵中含有xiaomi字符的个数,保证矩阵大小>=3;
    输入:
    4 4
    xiaq
    oime
    xaic
    cadf
    输出
    2

    思路:

    • 读入为char[][], 用nextline().toCharArray()给char[i]
    • 按行向下, 按列向右, 先cmp匹配一个初始的3*3; 如00–2,2;
    • row = 0, 往右, 限制为right
    • row的限制,i+3<=n, 即边界判断;

    代码块

    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int m = sc.nextInt();
            sc.nextLine();
            char[][] chars = new char[4][4];
            for (int i = 0; i < n; i++) {
                chars[i] = sc.nextLine().toCharArray();
            }
            System.out.println(solution(chars));
        }
    
    
        //3*3的matrix,xiaomi
        private static int solution(char[][] strings) {
    
            HashMap<Character, Integer> need = new HashMap<>();
            HashMap<Character, Integer> window = new HashMap<>();
            String p = "xiaomi";
            for (char c : p.toCharArray()) {
                need.put(c, need.getOrDefault(c, 0) + 1);
            }
    
    //        System.out.println(p);
    
            int left = 0, right = 0;//[)
            int valid = 0;
    
            int ans = 0;
    
            int k = 3;
            //00-33
    
            for (int i = 0; i+3 <= strings.length; i++) {
                window.clear();
                valid = cmp(i, strings, window, need, k);
                right = 3;left = 0;
                while (right<strings[0].length){
    //                System.out.println("valid=" + valid);
    
                    if (valid == need.size()) ans++;
                    //判断是否满足 然后移动
                    for (int j = i; j < i+3; j++) {
                        char c = strings[j][right];
                        if (need.containsKey(c)){
                            window.put(c,window.getOrDefault(c,0)+1);
                            if (window.get(c).equals(need.get(c))) valid++;
                        }
    
                        //移除
                        char d = strings[j][left];
                        if (need.containsKey(d)){
                            if (window.get(d).equals(need.get(d))) valid--;
                            window.put(d,window.get(d)-1);
                        }
                    }
                    right++;left++;
                }
            }
    
    
            return ans;
        }
    
        /**
         *
         * @param strings 原字符
         * @param window 窗口值
         * @param need 需要匹配
         * @param k 窗口长度
         */
        public static int cmp(int startX,char[][] strings,HashMap<Character,Integer> window,HashMap<Character,Integer> need,int k){
            //valid 满足字符数
            int valid = 0;
            for (int i = startX; i < startX+k; i++) {
                for (int j = 0; j < k; j++) {
                    char c = strings[i][j];
                    if (need.containsKey(c)){
                        window.put(c, window.getOrDefault(c, 0) + 1);
                        if (window.get(c).equals(need.get(c))) valid++;
                    }
                }
            }
            return valid;
        }
        private static void func(LinkedList<Integer> ans) {
            System.out.println(ans);
        }
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
  • 相关阅读:
    VB.NET三层之用户查询窗体
    YOLOv5识别图像内苹果和香蕉
    根据类的属性自动生成该属性名对应的字符串常量值
    Linux友好度太低?试试Clion远程开发|ssh连接远程主机
    vue3触发store的时机和使用store中的变量
    研究生拟录取分享
    C++ QT学习笔记
    学术团体的机器人相关分会和机器人相关大赛的说明
    golang 2018,go 1.19安装Gin
    WEB基础及http协议(Apache)
  • 原文地址:https://blog.csdn.net/weixin_42251246/article/details/133869341