U235862 最优评论

U235862 最优评论

按时间顺序给出n条评论 s i ​ 及相应的点赞数 t i ​ ,编号记为 i,评论有可能重复出现,对重复出现的两个评论,其点赞数有可能不同。 对于一条评论,如果此前从未出现过,称其为“原创评论”。 请找出点赞数最多的“原创评论”,输出其编号,如果存在多个评论满足条件,回答编号最小的。
输入格式
第一行一个整数表示n
接下来
n行,每行一个字符s i和一个整数t i
输出格式
点赞数最多的“原创评论”的编号,如有多个答案,输出最小的编号
输入输出样例
输入 #1复制

3
aaa 10
bbb 20
aaa 30

输出 #1复制
2
输入 #2复制

10
bb 3
ba 1
aa 4
bb 1
ba 5
aa 9
aa 2
ab 6
bb 5
ab 3

输出 #2复制
8
//在线等!!!!!

是这个意思不

img

#include <iostream>
#include <unordered_map>
using namespace std;

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

    unordered_map<string, int> commentMap;
    unordered_map<string, int> likeMap;
    int currentId = 1;
    int maxLikes = 0;
    int maxId = -1;

    for (int i = 0; i < n; i++) {
        string comment;
        int likes;
        cin >> comment >> likes;

        // 如果这是一条原创评论
        if (commentMap.find(comment) == commentMap.end()) {
            commentMap[comment] = currentId;
            likeMap[comment] = likes;
            currentId++;

            // 更新点赞数最多的评论
            if (likes > maxLikes) {
                maxLikes = likes;
                maxId = commentMap[comment];
            }
            else if (likes == maxLikes && commentMap[comment] < maxId) {
                maxId = commentMap[comment];
            }
        }
        // 如果这是一条重复的评论,但点赞数更高
        else if (likes > likeMap[comment]) {
            likeMap[comment] = likes;

            // 如果此时的点赞数更高,则更新最大值
            if (likes > maxLikes) {
                maxLikes = likes;
                maxId = commentMap[comment];
            }
            else if (likes == maxLikes && commentMap[comment] < maxId) {
                maxId = commentMap[comment];
            }
        }
    }

    cout << maxId << endl;

    return 0;
}


【以下回答由 GPT 生成】

#include <iostream>
#include <string>
#include <map>

int main() {
    int n;
    std::cin >> n;

    std::map<std::string, int> comments;
    std::map<int, std::string> originalComments;
    int maxLikes = 0;
    int originalCommentIndex = -1;

    for (int i = 0; i < n; i++) {
        std::string comment;
        int likes;
        std::cin >> comment >> likes;

        if (comments.find(comment) != comments.end()) {
            comments[comment] += likes;
        } else {
            comments[comment] = likes;
            originalComments[i] = comment;
            if (likes > maxLikes) {
                maxLikes = likes;
                originalCommentIndex = i;
            }
        }
    }

    for (const auto& originalComment : originalComments) {
        if (comments[originalComment.second] == maxLikes) {
            originalCommentIndex = originalComment.first;
            break;
        }
    }

    std::cout << originalCommentIndex << std::endl;

    return 0;
}

解题思路:

  1. 首先定义一个std::map来记录评论和对应的点赞数,以及一个std::map来记录原创评论的编号和评论内容。
  2. 使用一个循环读取用户输入的评论和点赞数,并分别存入两个std::map中。
  3. 在循环中,首先检查评论是否已经存在于commentsstd::map中,如果存在,则将点赞数累加。否则,将评论和点赞数分别添加到commentsoriginalCommentsstd::map中。
  4. 同时,检查当前点赞数是否大于最大点赞数maxLikes,如果是,则更新maxLikesoriginalCommentIndex
  5. 最后,遍历originalCommentsstd::map,找到点赞数等于maxLikes的评论的编号。
  6. 输出originalCommentIndex作为结果。


【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^