(新A卷,200分)- 垃圾短信识别(Java & JS & Python)华为OD

(新A卷,200分)- 垃圾短信识别(Java & JS & Python)华为OD

题目描述

大众对垃圾短信深恶痛绝,希望能对垃圾短信发送者进行识别,为此,很多软件增加了垃圾短信的识别机制。
经分析,发现正常用户的短信通常具备交互性,而垃圾短信往往都是大量单向的短信,按照如下规则进行垃圾短信识别:

本题中,发送者A符合以下条件之一的,则认为A是垃圾短信发送者:

  • A发送短信的接收者中,没有发过短信给A的人数L > 5;
  • A发送的短信数 – A接收的短信数M > 10;
  • 如果存在X,A发送给X的短信数 – A接收到X的短信数N > 5;

输入描述

第一行是条目数,接下来几行是具体的条目,每个条目,是一对ID,第一个数字是发送者ID,后面的数字是接收者ID,中间空格隔开,所有的ID都为无符号整型,ID最大值为100;

同一个条目中,两个ID不会相同(即不会自己给自己发消息)

最后一行为指定的ID

输出描述

输出该ID是否为垃圾短信发送者,并且按序列输出 L M 的值(由于 N 值不唯一,不需要输出);
输出均为字符串。

用例
输入15
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
14 1
1 15
1
输出true 13 13
说明true 表示1是垃圾短信发送者,两个数字,代表发送者1对应的L和M值。
true 13 13中间以一个空格分割。注意true是字符串输出。
输入15
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
14 1
1 15
2
输出false 0 -1
说明
题目解析

感觉是纯逻辑题,解析请看代码注释。

JavaScript算法源码
/* JavaScript Node ACM模式 控制台输入获取 */
const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines = [];
let n;
rl.on("line", (line) => {
  lines.push(line);

  if (lines.length === 1) {
    n = lines[0] - 0;
  }

  if (n && lines.length === n + 2) {
    lines.shift();
    const id = lines.pop();
    const arr = lines.map((line) => line.split(" "));

    console.log(getResult(id, arr));
    lines.length = 0;
  }
});

/**
 *
 * @param {*} tid 指定的ID
 * @param {*} arr [[发送者id,接收者id]]
 * @returns
 */
function getResult(tid, arr) {
  // send记录 tid发送短信给“哪些人”
  const send = [];
  // receive记录  “哪些人”发送短信给tid
  const receive = [];

  // sendCount记录 tid发送了“几次”(对象属性值)短信给某个人(对象属性)
  const sendCount = {};
  // receiveCount记录 某人(对象属性)发送了“几次”(对象属性值)短信给tid
  const receiveCount = {};

  for (let [sid, rid] of arr) {
    if (sid == tid) {
      send.push(rid);
      sendCount[rid] ? sendCount[rid]++ : (sendCount[rid] = 1);
    }
    if (rid == tid) {
      receive.push(sid);
      receiveCount[sid] ? receiveCount[sid]++ : (receiveCount[sid] = 1);
    }
  }

  const sendSet = new Set(send);
  const receiveSet = new Set(receive);
  // connect记录和tid有交互的id
  const connect = [...sendSet].filter((id) => receiveSet.has(id));

  const l = sendSet.size - connect.length;
  const m = send.length - receive.length;

  let isSpammers = l > 5 || m > 10;

  // 如果已经通过l和m确定了tid是垃圾短信发送者,那就不需要再确认n了,否则还是需要确认n
  if (!isSpammers) {
    // for (let id of connect) {
    //   if ((sendCount[id] ?? 0) - (receiveCount[id] ?? 0) > 5) {
    // 根据网友指正,第三条规则中,X不一定非要和A存在交互,即存在A发送6条短信给X,但是X没有发送过短信给A,此时也符合第三条规则
    // 因此,这里id应该遍历自sendSet,而不是connect
    for (let id of sendSet) {
      if (sendCount[id] - (receiveCount[id] ?? 0) > 5) {
        // 一旦发现x,则可以判断,则确定tid是垃圾短信发送者,可提前结束循环
        isSpammers = true;
        break;
      }
    }
  }

  return `${isSpammers} ${l} ${m}`;
}

Java算法源码
import java.util.*;
import java.util.stream.Collectors;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int n = sc.nextInt();

    int[][] arr = new int[n][2];
    for (int i = 0; i < n; i++) {
      arr[i][0] = sc.nextInt();
      arr[i][1] = sc.nextInt();
    }

    int id = sc.nextInt();

    System.out.println(getResult(id, arr));
  }

  public static String getResult(int tid, int[][] arr) {
    // send记录 tid发送短信给“哪些人”
    LinkedList<Integer> send = new LinkedList<>();
    // receive记录  “哪些人”发送短信给tid
    LinkedList<Integer> receive = new LinkedList<>();

    // sendCount记录 tid发送了“几次”(对象属性值)短信给某个人(对象属性)
    HashMap<Integer, Integer> sendCount = new HashMap<>();
    // receiveCount记录 某人(对象属性)发送了“几次”(对象属性值)短信给tid
    HashMap<Integer, Integer> receiveCount = new HashMap<>();

    for (int[] ele : arr) {
      int sid = ele[0];
      int rid = ele[1];

      if (sid == tid) {
        send.addLast(rid);
        sendCount.put(rid, sendCount.getOrDefault(rid, 0) + 1);
      }

      if (rid == tid) {
        receive.addLast(sid);
        receiveCount.put(sid, receiveCount.getOrDefault(sid, 0) + 1);
      }
    }

    HashSet<Integer> sendSet = new HashSet<>(send);
    HashSet<Integer> receiveSet = new HashSet<>(receive);

    // connect记录和tid有交互的id
    List<Integer> connect =
        sendSet.stream().filter(receiveSet::contains).collect(Collectors.toList());

    int l = sendSet.size() - connect.size();
    int m = send.size() - receive.size();

    boolean isSpammers = l > 5 || m > 10;

    // 如果已经通过l和m确定了tid是垃圾短信发送者,那就不需要再确认n了,否则还是需要确认n
    if (!isSpammers) {
      //      for (Integer id : connect) {
      //        if (sendCount.getOrDefault(id, 0) - receiveCount.getOrDefault(id, 0) > 5) {
      // 根据网友指正,第三条规则中,X不一定非要和A存在交互,即存在A发送6条短信给X,但是X没有发送过短信给A,此时也符合第三条规则
      // 因此,这里id应该遍历自sendSet,而不是connect
      for (Integer id : sendSet) {
        if (sendCount.get(id) - receiveCount.getOrDefault(id, 0) > 5) {
          // 一旦发现x,则可以判断,则确定tid是垃圾短信发送者,可提前结束循环
          isSpammers = true;
          break;
        }
      }
    }

    return isSpammers + " " + l + " " + m;
  }
}

Python算法源码
# 输入获取
n = int(input())
arr = [input().split() for i in range(n)]
tid = input()


# 算法入口
def getResult(tid, arr):
    # send记录 tid发送短信给“哪些人”
    send = []
    # receive记录  “哪些人”发送短信给tid
    receive = []

    # sendCount记录 tid发送了“几次”(对象属性值)短信给某个人(对象属性)
    sendCount = {}
    # receiveCount记录 某人(对象属性)发送了“几次”(对象属性值)短信给tid
    receiveCount = {}

    for sid, rid in arr:
        if sid == tid:
            send.append(rid)
            if sendCount.get(rid) is None:
                sendCount[rid] = 1
            else:
                sendCount[rid] += 1

        if rid == tid:
            receive.append(sid)
            if receiveCount.get(sid) is None:
                receiveCount[sid] = 1
            else:
                receiveCount[sid] += 1

    sendSet = set(send)
    receiveSet = set(receive)

    # connect记录和tid有交互的id
    connect = list(filter(lambda x: x in receiveSet, list(sendSet)))

    l = len(sendSet) - len(connect)
    m = len(send) - len(receive)

    isSpammers = l > 5 or m > 10

    # 如果已经通过l和m确定了tid是垃圾短信发送者,那就不需要再确认n了,否则还是需要确认n
    if not isSpammers:
        # for id in connect:
        #     if (sendCount.get(id) or 0) - (receiveCount.get(id) or 0) > 5:
        # 根据网友指正,第三条规则中,X不一定非要和A存在交互,即存在A发送6条短信给X,但是X没有发送过短信给A,此时也符合第三条规则
        # 因此,这里id应该遍历自sendSet,而不是connect
        for id in sendSet:
            if sendCount.get(id) - (receiveCount.get(id) or 0) > 5:
                isSpammers = True
                # 一旦发现x,则可以判断,则确定tid是垃圾短信发送者,可提前结束循环
                break

    return f"{str(isSpammers).lower()} {l} {m}"


# 算法调用
print(getResult(tid, arr))
&lt;think&gt;嗯,用户想了解华为OD机试的题目类型和解题思路,还有准备资料。首先,我需要回忆之前用户提供的引用内容,虽然引用里具体题目内容被隐藏了,但可以推断出华为OD机试可能涉及编程题、算法题等类型。用户之前的问题可能涉及数据类、Wonderland题目、员工考试等,这些可能对应不同的题型。 用户可能需要知道常见的题型有哪些,比如数据结构、算法应用、字符串处理、动态规划等。然后解题思路方面,可能需要步骤讲解如何析问题,选择合适的数据结构,优化算法效率等。准备资料的话,可能需要推荐一些学习资源,比如真题汇总、在线练习平台等。 要注意用户可能对JavaPython、C++、JS等不同语言的实现感兴趣,所以需要提到不同语言的解题参考。另外,用户可能关心时间复杂度和空间复杂度的优化,这也是机试中常考的点。比如快速排序的性能析,或者动态规划的应用场景。 还需要检查是否有遗漏的题型,比如可能还有二叉树、图论相关的题目。同时,解题技巧如双指针、滑动窗口、回溯法等是否应该提到。另外,测试用例的覆盖和边界条件处理也是重要部,需要提醒用户注意。 最后,生成的相关问题要覆盖用户可能进一步询问的方向,比如具体题型的例子、如何高效练习,或者不同语言在解题中的差异。确保回答结构清晰,点列出,便于用户理解。&lt;/think&gt;# 华为OD机试题目类型及解题思路指南 ## 一、常见题目类型及示例 1. **数据结构应用** - 链表操作:合并有序链表链表反转 - 树结构:二叉树遍历、最近公共祖先 - 示例代码片段: ```python # 链表节点定义 class ListNode: def __init__(self, val=0): self.val = val self.next = None ``` 2. **算法设计** - 排序算法:快速排序优化(时间复杂度$O(n \log n)$) - 动态规划:背包问题、最长子序列 - 治法:归并排序(空间复杂度$O(n)$) 3. **字符串处理** - 模式匹配:KMP算法优化 - 回文判断:中心扩展法 - 示例问题:字符串压缩(如`aaabbbcc`&rarr;`a3b3c2`) 4. **数学逻辑题** - 数位运算:二进制中1的计数 - 几何计算:坐标点距离公式$d=\sqrt{(x_2-x_1)^2 + (y_2-y_1)^2}$ ## 二、解题思路框架 1. **问题析阶段** - 明确输入输出格式(如引用[2]中Wonderland题目要求特定格式输出) - 识别隐藏条件:如数据范围限制(常见$1 \leq n \leq 10^5$) 2. **算法选择** | 问题特征 | 推荐算法 | 时间复杂度 | |---|---|--| | 有序数据查询 | 二查找 | $O(\log n)$ | | 最短路径 | BFS | $O(n+m)$ | | 子数组极值 | 滑动窗口 | $O(n)$ | 3. **代码实现要点** - 边界处理:如空输入、极值测试用例 - 优化技巧:使用哈希表减少嵌套循环(参考引用[1]数据类问题) ## 三、备考资源推荐 1. **真题训练** - 华为OD真题汇总(Java/Python/C++/JS多语言版本)[^1][^2][^3] - 高频考点:递归、DFS、位运算(出现概率>60%) 2. **学习平台** - LeetCode华为专项练习(标注华为真题标签) - 《剑指Offer》动态规划章节(重点阅读) 3. **模拟考试工具** - 使用IDEA/VS Code配置OJ环境 - 华为机试模拟系统(支持在线调试)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值