【阿里笔试题汇总】[全网首发] 2024-04-29-阿里国际春招笔试题-三语言题解(CPP/Python/Java)

🍭 大家好这里是清隆学长 ,一枚热爱算法的程序员

✨ 本系列打算持续跟新阿里国际近期的春秋招笔试题汇总~

💻 ACM银牌🥈| 多次AK大厂笔试 | 编程一对一辅导

👏 感谢大家的订阅➕ 和 喜欢💗

📧 清隆这边最近正在收集近一年互联网各厂的笔试题汇总,如果有需要的小伙伴可以关注后私信一下 清隆领取,会在飞书进行同步的跟新。

⌚️ 春招笔试也是接近尾声,没有拿到offer的小伙伴们抓紧啦~

💞 01.卢小姐的连续音节挑战

问题描述

卢小姐最近在学习外语,她注意到在某些语言中,元音不能连续出现。现在,她有一串由不同字母组成的字符序列,她想找出一个最长的连续子序列,使得在这个子序列中没有两个相邻的元音。元音定义为 “a”, “e”, “i”, “o”, “u” 这五个字母,其他所有字母都被视为辅音。

输入格式

第一行包含一个正整数 n n n,代表卢小姐拿到的字符序列的长度。

第二行包含一个长度为 n n n 的字符串,代表卢小姐拿到的字符序列。

输出格式

输出一个整数,代表卢小姐可以得到的最长子序列的长度。

样例输入

7
aeiobcd

样例输出

4

数据范围

1 ≤ n ≤ 200000 1 \leq n \leq 200000 1n200000

题解

可以遍历整个字符串,同时跟踪当前连续子序列的长度。当遇到元音时,需要检查前一个字符是否也是元音。如果是,就重置当前子序列的长度;如果不是,就继续增加当前子序列的长度,并更新最大长度。

参考代码

  • Python
def longest_subsequence_without_adjacent_vowels(n, s):
    vowels = set("aeiou")
    max_length = 0
    current_length = 0
    prev_vowel = False
    
    for i in range(n):
        if s[i] in vowels:
            if prev_vowel:
                current_length = 1  # 重置,因为前一个是元音
            else:
                current_length += 1
            prev_vowel = True
        else:
            current_length += 1
            prev_vowel = False
        
        max_length = max(max_length, current_length)
    
    return max_length

# 读取输入数据
n = int(input().strip())
s = input().strip()

# 输出最长子序列长度
print(longest_subsequence_without_adjacent_vowels(n, s))
  • Java
import java.util.*;

public class Main {
    public static int longestSubsequenceWithoutAdjacentVowels(int n, String s) {
        Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u'));
        int max_length = 0;
        int current_length = 0;
        boolean prev_vowel = false;

        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            if (vowels.contains(c)) {
                if (prev_vowel) {
                    current_length = 1;  // Reset because previous character was a vowel
                } else {
                    current_length += 1;
                }
                prev_vowel = true;
            } else {
                current_length += 1;
                prev_vowel = false;
            }
            max_length = Math.max(max_length, current_length);
        }

        return max_length;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine(); // Consume newline
        String s = scanner.nextLine();

        System.out.println(longestSubsequenceWithoutAdjacentVowels(n, s));
    }
}
  • Cpp
#include <iostream>
#include <string>
#include <set>
using namespace std;

int longest_subsequence_without_adjacent_vowels(int n, const string& s) {
    set<char> vowels = {'a', 'e', 'i', 'o', 'u'};
    int max_length = 0;
    int current_length = 0;
    bool prev_vowel = false;

    for (int i = 0; i < n; ++i) {
        if (vowels.count(s[i])) {
            if (prev_vowel) {
                current_length = 1;  // Reset because previous character was a vowel
            } else {
                current_length += 1;
            }
            prev_vowel = true;
        } else {
            current_length += 1;
            prev_vowel = false;
        }
        max_length = max(max_length, current_length);
    }

    return max_length;
}

int main() {
    int n;
    string s;
    cin >> n;
    cin >> s;

    cout << longest_subsequence_without_adjacent_vowels(n, s) << endl;
    return 0;
}

💓 02.K小姐的魔法药水

题目描述

K小姐是一位魔法师,她发明了一种神奇的魔法药水。这种药水的效果取决于配方中的魔法材料。每种材料都有一个魔力值 a i a_i ai,药水的最终魔力等于所有材料魔力值的乘积,即 f ( a 1 ) × f ( a 2 ) × … × f ( a n ) f(a_1) \times f(a_2) \times \ldots \times f(a_n) f(a1)×f(a2)××f(an),其中 f ( x ) = x x f(x) = x^x f(x)=xx

现在,K小姐准备了 n n n 种魔法材料,她想知道最终药水的魔力有多少种不同的因子。由于答案可能很大,请对 1 0 9 + 7 10^9+7 109+7 取模。

输入格式

第一行包含一个正整数 n n n,表示魔法材料的种类数。

第二行包含 n n n 个正整数 a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an,表示每种魔法材料的魔力值。

输出格式

输出一个整数,表示最终药水魔力的因子个数对 1 0 9 + 7 10^9+7 109+7 取模的结果。

样例输入

3
1 2 3

样例输出

12

数据范围

1 ≤ n ≤ 2 × 1 0 5 1 \le n \le 2 \times 10^5 1n2×105
1 ≤ a i ≤ 2 × 1 0 5 1 \le a_i \le 2 \times 10^5 1ai2×105

题解

本题可以通过分解质因数的方法来求解。

首先,对于每个魔力值 a i a_i ai,计算 f ( a i ) = a i a i f(a_i) = a_i^{a_i} f(ai)=aiai。然后将所有的 f ( a i ) f(a_i) f(ai) 进行质因数分解,统计每个质因子的指数之和。最终答案等于所有质因子指数加一的乘积。

具体步骤如下:

  1. 遍历每个魔力值 a i a_i ai
    • a i a_i ai 进行质因数分解,对于每个质因子 p p p,计算 a i a_i ai p p p 的指数 c n t cnt cnt,将 c n t × a i cnt \times a_i cnt×ai 累加到 p p p 的总指数中。
  2. 将所有质因子的总指数加一,并累乘得到最终答案。
  3. 将答案对 1 0 9 + 7 10^9+7 109+7 取模。

时间复杂度为 O ( n a i ) O(n \sqrt{a_i}) O(nai ),空间复杂度为 O ( a i ) O(a_i) O(ai)

参考代码

  • Python
import sys
from collections import Counter

def main():
    mod = 10 ** 9 + 7
    n = int(sys.stdin.readline())
    a = list(map(int, sys.stdin.readline().split()))
    
    factors = Counter()
    for x in a:
        for i in range(2, int(x ** 0.5) + 1):
            if x % i == 0:
                cnt = 0
                while x % i == 0:
                    cnt += 1
                    x //= i
                factors[i] += cnt * x
        if x > 1:
            factors[x] += x
    
    res = 1
    for exp in factors.values():
        res = res * (exp + 1) % mod
    
    print(res)

if __name__ == '__main__':
    main()
  • Java
import java.io.*;
import java.util.*;

public class Main {
    private static final int MOD = (int) 1e9 + 7;
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] a = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        
        Map<Integer, Integer> factors = new HashMap<>();
        for (int x : a) {
            for (int i = 2; i <= Math.sqrt(x); i++) {
                if (x % i == 0) {
                    int cnt = 0;
                    while (x % i == 0) {
                        cnt++;
                        x /= i;
                    }
                    factors.put(i, factors.getOrDefault(i, 0) + cnt * x);
                }
            }
            if (x > 1) {
                factors.put(x, factors.getOrDefault(x, 0) + x);
            }
        }
        
        long res = 1;
        for (int exp : factors.values()) {
            res = res * (exp + 1) % MOD;
        }
        
        System.out.println(res);
    }
}
  • Cpp
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
using ll = long long;

const int MOD = 1e9 + 7;

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    
    unordered_map<int, long long> factors;
    for (int x : a) {
        for (int i = 2; i * i <= x; i++) {
            if (x % i == 0) {
                int cnt = 0;
                while (x % i == 0) {
                    cnt++;
                    x /= i;
                }
                factors[i] +=1ll * cnt * x;
            }
        }
        if (x > 1) {
            factors[x] += x;
        }
    }
    
    ll res = 1;
    for (auto [p, exp] : factors) {
        res = res * (exp + 1) % MOD;
    }
    
    cout << res << endl;
    
    return 0;
}

💖 03.彩色项链

题目描述

LYA 有一条由 n n n 个珠子组成的项链,每个珠子都有一个颜色,用一个大写字母表示。现在 LYA 想知道,在这条项链上一共有多少种不同的简单路径,恰好包含 k k k 种颜色的珠子。

一个简单路径指的是项链上的一段连续的珠子序列,且任意两个珠子之间至多只有一条边相连。

输入格式

第一行包含两个正整数 n n n k k k,分别表示项链的珠子数和需要的颜色数。

第二行是一个长度为 n n n 的字符串,表示项链上每个珠子的颜色。

接下来 n − 1 n-1 n1 行,每行两个整数 u , v u,v u,v,表示编号为 u u u v v v 的珠子之间有一条边相连。

输出格式

输出一个整数,表示满足条件的简单路径的数目。

样例输入

3 2
RBG
1 2
2 3

样例输出

2

数据范围

  • 1 ≤ n ≤ 1 0 3 1 \leq n \leq 10^3 1n103
  • 1 ≤ k ≤ 26 1 \leq k \leq 26 1k26
  • 字符串中只包含大写字母
  • 数据保证给出的是一棵树

题解

本题可以用 DFS 来解决。我们从每个点出发,进行一次 DFS 遍历,过程中维护当前路径上出现的颜色种类数。如果超过了 k k k 种,就直接返回;如果恰好为 k k k 种,就将答案加 1 1 1

注意到每条路径都会被计算两次,因为它的两个端点都会作为起点进行一次 DFS。所以最后的答案需要除以 2 2 2。特殊地,如果 k = 1 k=1 k=1,不需要除以 2 2 2

时间复杂度 O ( n 2 ) O(n^2) O(n2),空间复杂度 O ( n ) O(n) O(n)

参考代码

  • Python
import sys
input = lambda: sys.stdin.readline().strip()
from collections import Counter

def dfs(u: int, pre: int, s: Counter):
    global res
    if len(s) > k:
        return
    if len(s) == k:
        res += 1
    for i in g[u]:
        if i != pre:
            s[color[i-1]] += 1
            dfs(i, u, s)
            s[color[i-1]] -= 1
            if s[color[i-1]] == 0:
                del s[color[i-1]]

n, k = map(int, input().split())
color = input()
g = [[] for _ in range(n+1)]

for _ in range(n-1):
    a, b = map(int, input().split())
    g[a].append(b)
    g[b].append(a)

res = 0
for i in range(1, n+1):
    cnt = Counter()
    cnt[color[i-1]] = 1
    dfs(i, -1, cnt)

if k != 1:
    res //= 2
print(res)
  • Java
import java.io.*;
import java.util.*;

public class Main {
    static int n, k;
    static String color;
    static List<Integer>[] g;
    static int res;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] str = br.readLine().split(" ");
        n = Integer.parseInt(str[0]);
        k = Integer.parseInt(str[1]);
        color = br.readLine();
        g = new List[n+1];
        for (int i = 1; i <= n; i++) {
            g[i] = new ArrayList<>();
        }
        for (int i = 0; i < n-1; i++) {
            str = br.readLine().split(" ");
            int a = Integer.parseInt(str[0]);
            int b = Integer.parseInt(str[1]);
            g[a].add(b);
            g[b].add(a);
        }
        for (int i = 1; i <= n; i++) {
            Map<Character, Integer> cnt = new HashMap<>();
            cnt.put(color.charAt(i-1), 1);
            dfs(i, -1, cnt);
        }
        if (k != 1) {
            res /= 2;
        }
        System.out.println(res);
    }

    static void dfs(int u, int pre, Map<Character, Integer> cnt) {
        if (cnt.size() > k) {
            return;
        }
        if (cnt.size() == k) {
            res++;
        }
        for (int i : g[u]) {
            if (i != pre) {
                char c = color.charAt(i-1);
                cnt.put(c, cnt.getOrDefault(c, 0)+1);
                dfs(i, u, cnt);
                cnt.put(c, cnt.get(c)-1);
                if (cnt.get(c) == 0) {
                    cnt.remove(c);
                }
            }
        }
    }
}
  • Cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;

int n, k;
string color;
vector<int> g[N];
int res;

void dfs(int u, int pre, unordered_map<char, int>& cnt) {
    if (cnt.size() > k) {
        return;
    }
    if (cnt.size() == k) {
        res++;
    }
    for (int i : g[u]) {
        if (i != pre) {
            char c = color[i-1];
            cnt[c]++;
            dfs(i, u, cnt);
            if (--cnt[c] == 0) {
                cnt.erase(c);
            }
        }
    }
}

int main() {
    cin >> n >> k;
    cin >> color;
    for (int i = 0; i < n-1; i++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for (int i = 1; i <= n; i++) {
        unordered_map<char, int> cnt;
        cnt[color[i-1]] = 1;
        dfs(i, -1, cnt);
    }
    if (k != 1) {
        res /= 2;
    }
    cout << res << endl;
    return 0;
}

写在最后

📧 清隆这边最近正在收集近一年互联网各厂的笔试题汇总,如果有需要的小伙伴可以关注后私信一下 清隆领取,会在飞书进行同步的跟新。

在这里插入图片描述
在这里插入图片描述

  • 21
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
2023年3月11日,美团春季招聘笔试中共包含五道编程题目。以下是对每道题目的简要说明: 1. 题目一:这道题目要求解决一个数字统计的问题。可能涉及到的知识点包括数据结构、循环和条件判断等。解决问题的思路可能是使用字典等数据结构来保存统计结果,并使用循环逐个读取输入数据并进行统计。 2. 题目二:这道题目可能是一个字符串处理的问题。需要使用字符串的方法进行操作,如提取、拼接、查找和替换等。可能的解决思路包括使用正则表达式、切片和遍历等。 3. 题目:这道题目可能涉及到算法和数据结构的知识。可能是一道涉及到数组、链表、树等数据结构的问题。解决思路可能包括遍历、递归、搜索和排序等。 4. 题目四:这道题目可能是一个动态规划的问题。需要根据给定的条件和规则,通过动态规划的方式求解问题。解决思路包括定义状态和转移方程,使用递推或记忆化搜索进行求解。 5. 题目五:这道题目可能是一个图论或网络问题。需要根据给定的图或网络结构,解决一个相关的问题。可能涉及到广度优先搜索、深度优先搜索、最短路径等知识。解决思路可能包括使用图或网络的相关算法进行求解。 以上只是对这五道编程题目的一些可能情况进行的简要描述,具体的题目内容可能会有所不同。希望这些信息能对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值