笔试题 360 22春招 技术综合A卷 算法题

1. 字符串匹配

已知字符串s(只有小写字母组成),求s中的子串在s中拥有的最大数量

s = aba
字串: (a) (b) (ab) (ba) (aba)
a 出现次数最多为两次,输出2

思路

如果一个子串出现n次,那么这个子串的子串也能重复n次
所以,实际上就是求哪个字母最多

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        int[] ans = new int[30];
        int max = 0;
        for (int i = 0; i < str.length(); i++) {
            int c = str.charAt(i) - '0' - 49;
            ans[c] ++;
            max = Math.max(max,ans[c]);
        }
    }
}

2. 货物堆积

在这里插入图片描述
输入一个数n,代表(R -2 ) * (C - 1) * (L - 2)

例:

输入:n = 4,输出 41
R = 3 ; C = 5; L = 3;
1 <= n <= 1e9

思路

想到了暴力,因为 R * C * L ~= n 理论上来说是O(n) ,应该不会超时,但是只过了55% 的数据

class Main2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long max = 0L;
        for (int i = 3; i < n + 3; i++) {
            if (i - 2 > n) break;
            for (int j = 2; j < Integer.MAX_VALUE; j++) {
                if ((i - 2) * (j - 1) > n) break;
                for (int k = 3; k < Integer.MAX_VALUE; k++) {
                    if ((i - 2) * (j - 1) * (k - 2) > n) {
                        break;
                    } else if ((i - 2) * (j - 1) * (k - 2) == n) {
                        max = Math.max(max, (long) i * j * k - n);
                    }
                }
            }
        }
        System.out.println(max);
    }
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值