华为OD机试真题 新系统 2026-06-10 JavaGoC 实现【双系统资源类型调配】【200】

发布时间:2026/6/14 1:14:02

华为OD机试真题 新系统 2026-06-10 JavaGoC 实现【双系统资源类型调配】【200】 目录题目思路Code题目给定两个仅由小写字母组成的字符串四resA和resB。你可以执行恰好一次操作:选择下标i和j交换resA[i]与resB[j]。问是否存在一种交换使交换后两个字符串中不同字符的数量相等。如果存在该交换则返回交换后resA和resB中不同字符的数量;如果有多种交换方式则采用让两个字符串中不同的字符的数量最少的方式。如果不存在任何交换方式则返回-1。提示:· 1 resA.length,resB.length 10^5resA和resB仅由小写英文字母组成样例1输入acb输出-1说明交换任何一组下标都会导致第一个字符串中有2个不同的字符而在第二个字符串中只有1个不同字符。故无法完成返回-1。样例2输入abccaab输出2说明交换第一个字符串的下标1和第二个字符串的下标0。之后得到word1“aacc”和word2“abb”各有2个不同字符。思路枚举所有字符对因为只有 26 个小写字母可以枚举所有(a, b)字符对其中cntA[a] 0且cntB[b] 0。对每对(a, b)如果a b交换相同字符字符串不变deltaA deltaB 0如果a ! bA 失去a若cntA[a] 1唯一一个distinctA 减 1A 获得b若cntA[b] 0A 原来没有 bdistinctA 加 1B 失去b若cntB[b] 1distinctB 减 1B 获得a若cntB[a] 0distinctB 加 1计算 deltaA 和 deltaB如果distinctA deltaA distinctB deltaB就是有效交换。在有效交换中取最小的相等不同字符数。时间 O(n 26²)空间 O(26)。Codeimport java.util.Scanner; /** * 交换字符使不同字符数相等 * 输入两行分别为 resA 和 resB * 输出一个整数表示相等的最小不同字符数不存在则返回 -1 */ public class Main { public static void main(String[] args) { try (Scanner sc new Scanner(System.in)) { String resA sc.nextLine(); String resB sc.nextLine(); System.out.println(solve(resA, resB)); } catch (Exception e) { System.out.println(-1); } } static int solve(String resA, String resB) { // ---- 统计字符频率 ---- int[] cntA new int[26]; int[] cntB new int[26]; for (char c : resA.toCharArray()) cntA[c - a]; for (char c : resB.toCharArray()) cntB[c - a]; // ---- 计算当前不同字符数 ---- int distinctA 0, distinctB 0; for (int i 0; i 26; i) { if (cntA[i] 0) distinctA; if (cntB[i] 0) distinctB; } // ---- 枚举所有可能的字符对 (a, b) ---- int best Integer.MAX_VALUE; for (int a 0; a 26; a) { if (cntA[a] 0) continue; // a 在 A 中不存在 for (int b 0; b 26; b) { if (cntB[b] 0) continue; // b 在 B 中不存在 int deltaA, deltaB; if (a b) { // 交换相同字符字符串不变 deltaA 0; deltaB 0; } else { // A 失去 a若 A 中只有这一个 a不同字符数减 1 // A 获得 b若 A 原来没有 b不同字符数加 1 deltaA (cntA[a] 1 ? 0 : -1) (cntA[b] 0 ? 0 : 1); // B 失去 b若 B 中只有这一个 b不同字符数减 1 // B 获得 a若 B 原来没有 a不同字符数加 1 deltaB (cntB[b] 1 ? 0 : -1) (cntB[a] 0 ? 0 : 1); } // 判断交换后是否相等 if (distinctA deltaA distinctB deltaB) { int result distinctA deltaA; if (result best) best result; } } } return best Integer.MAX_VALUE ? -1 : best; } }Gopackage main import ( bufio fmt math os strings ) /** * 交换字符使不同字符数相等 * param resA 第一个字符串 * param resB 第二个字符串 * return 相等的最小不同字符数不存在返回 -1 */ func solve(resA, resB string) int { // ---- 统计字符频率 ---- cntA : make([]int, 26) cntB : make([]int, 26) for _, ch : range resA { cntA[ch-a] } for _, ch : range resB { cntB[ch-a] } // ---- 计算当前不同字符数 ---- distinctA : 0 distinctB : 0 for i : 0; i 26; i { if cntA[i] 0 { distinctA } if cntB[i] 0 { distinctB } } // ---- 枚举所有可能的字符对 (a, b) ---- best : math.MaxInt32 for a : 0; a 26; a { if cntA[a] 0 { continue // a 在 A 中不存在 } for b : 0; b 26; b { if cntB[b] 0 { continue // b 在 B 中不存在 } var deltaA, deltaB int if a b { // 交换相同字符字符串不变 deltaA 0 deltaB 0 } else { // A 失去 a若 A 中只有这一个 a不同字符数减 1 // A 获得 b若 A 原来没有 b不同字符数加 1 if cntA[a] 1 { deltaA 0 } else { deltaA -1 } if cntA[b] 0 { deltaA } // B 失去 b若 B 中只有这一个 b不同字符数减 1 // B 获得 a若 B 原来没有 a不同字符数加 1 if cntB[b] 1 { deltaB 0 } else { deltaB -1 } if cntB[a] 0 { deltaB } } // 判断交换后是否相等 if distinctAdeltaA distinctBdeltaB { result : distinctA deltaA if result best { best result } } } } if best math.MaxInt32 { return -1 } return best } func main() { defer func() { if r : recover(); r ! nil { fmt.Println(-1) } }() scanner : bufio.NewScanner(os.Stdin) scanner.Scan() resA : strings.TrimSpace(scanner.Text()) scanner.Scan() resB : strings.TrimSpace(scanner.Text()) fmt.Println(solve(resA, resB)) }C#include stdio.h #include string.h #include limits.h /** * 交换字符使不同字符数相等 * param resA 第一个字符串 * param resB 第二个字符串 * return 相等的最小不同字符数不存在返回 -1 */ int solve(const char* resA, const char* resB) { // ---- 统计字符频率 ---- int cntA[26] {0}, cntB[26] {0}; for (int i 0; resA[i]; i) cntA[resA[i] - a]; for (int i 0; resB[i]; i) cntB[resB[i] - a]; // ---- 计算当前不同字符数 ---- int distinctA 0, distinctB 0; for (int i 0; i 26; i) { if (cntA[i] 0) distinctA; if (cntB[i] 0) distinctB; } // ---- 枚举所有可能的字符对 (a, b) ---- int best INT_MAX; for (int a 0; a 26; a) { if (cntA[a] 0) continue; // a 在 A 中不存在 for (int b 0; b 26; b) { if (cntB[b] 0) continue; // b 在 B 中不存在 int deltaA, deltaB; if (a b) { // 交换相同字符字符串不变 deltaA 0; deltaB 0; } else { // A 失去 a若 A 中只有这一个 a不同字符数减 1 // A 获得 b若 A 原来没有 b不同字符数加 1 deltaA (cntA[a] 1 ? 0 : -1) (cntA[b] 0 ? 0 : 1); // B 失去 b若 B 中只有这一个 b不同字符数减 1 // B 获得 a若 B 原来没有 a不同字符数加 1 deltaB (cntB[b] 1 ? 0 : -1) (cntB[a] 0 ? 0 : 1); } // 判断交换后是否相等 if (distinctA deltaA distinctB deltaB) { int result distinctA deltaA; if (result best) best result; } } } return best INT_MAX ? -1 : best; } int main() { char resA[100005], resB[100005]; if (fgets(resA, sizeof(resA), stdin) NULL) { printf(-1\n); return 0; } int lenA strlen(resA); if (lenA 0 resA[lenA - 1] \n) resA[lenA - 1] \0; if (lenA 1 resA[lenA - 2] \r) resA[lenA - 2] \0; if (fgets(resB, sizeof(resB), stdin) NULL) { printf(-1\n); return 0; } int lenB strlen(resB); if (lenB 0 resB[lenB - 1] \n) resB[lenB - 1] \0; if (lenB 1 resB[lenB - 2] \r) resB[lenB - 2] \0; printf(%d\n, solve(resA, resB)); return 0; }【华为od机试真题PythonJSJavaGo合集】【超值优惠】Py/JS/Java/Go合集【华为od机试真题Python】Python真题题库【华为od机试真题JavaScript】JavaScript真题题库【华为od机试真题JavaGo】JavaGo真题题库【华为od机试真题C】C真题题库【华为od机试真题C语言】C语言真题题库【华为od面试手撕代码题库】面试手撕代码题库【华为od机试面试交流群】【文章底部有二维码链接可扫码加交流群】华为OD机试:二本院校有机会吗?有机会,但不大,大神除外!机考分数越高越好,所以需要提前刷题。机考通过后,如果没有收到面试邀请,也不要着急,非目标院校面试邀请发的时间比较晚。非目标院校今年有点难,机试至少要考到350分,所以需要疯狂刷题,华为OD机考是有题库的,最好在考前完所有题库题目。华为OD机试:跨专业可以参加华为OD可以,但是如果你的本科院校比较差,上岸概率不大。华为OD机试:华为OD简历被锁定机试通过,性格测试也通过,但是没人联系面试,发现简历被锁定。此时需要主动去联系HR。让他帮助你查询原因。

相关新闻