鱼C论坛

 找回密码
 立即注册
查看: 1068|回复: 5

一道C++题目求助,我的代码到底哪里出了问题

[复制链接]
发表于 2023-4-22 19:54:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 zhangjinxuan 于 2023-6-8 14:20 编辑

中文题目
OLO的全球锦标赛ISM正在如火如荼地进行,Shizuku是参加锦标赛的GNR的忠实粉丝。OLO是一种由五名玩家组成的两支队伍相互比赛的游戏。PENTA KILL在游戏中被认为是一个令人难以置信的成就,这意味着一名玩家连续杀死五个成对的不同对手。我们假设一个玩家在死后会立即复活,并且死亡不会影响对其PENTA KILL的判决。

通常情况下,PENTA KILL将显示在游戏中。然而,有时由于竞争团队之间的延迟存在意外差异,因此无法在游戏中正确显示。游戏结束后,Shizuku会得到一份按时间顺序排列的游戏中的击杀列表。她想知道一个玩家是否在这个游戏中获得了PENTA KILL。

输入

第一行包含一个整数n(1≤n≤1000),表示游戏中的击杀次数。

以下n行中的每一行都包含两个字符串a和b,由英文字母和数字组成,表示名为a的玩家杀死了名为b的玩家。
每个字符串的长度不会超过100,可以保证队友之间不会发生杀戮,并且每支球队只有五名球员。

输出

如果玩家打成了 “五杀”,请输出 "PENTA KILL!",否则输出 "SAD:("

英文题目

OLO's global tournament, ISM, is in full swing and Shizuku is a big fan of GNR which is taking part in the tournament. OLO is a game where two teams of five players play against each other. PENTA KILL is considered an unbelievable achievement in the game, which means one player kills five pairwise distinct opponents in a row. We assume that a player will be resurrected immediately after his death, and the death will not affect the verdict of his PENTA KILL.

Normally, PENTA KILL will be displayed in the game. However, sometimes due to unintended disparity in latency between competing teams, it is not displayed properly in the game. After the game, Shizuku gets a chronological list of kills during the game. She wants to know whether a player has achieved PENTA KILL in this game.

Input
The first line contains an integer n(1≤n≤1000), indicating the number of kills in the game.

Each of the following n lines contains two strings a and b consisting of English letters and digital numbers, indicating that the player named a kills the player named b. The length of each string won't exceed 100. It is guaranteed that there are no kills between teammates and there are exactly five players per team.

Output
Output PENTA KILL! if a player has achieved "PENTA KILL", or "SAD:(" otherwise.

我的代码
#include <bits/stdc++.h>
using namespace std;

int t, slen, tlen;
string s1, t2;
map<string, map<string, bool>> tmp;
map<string, int> cnt;

int main() {
        scanf("%d", &t);
        while(t--) {
                cin >> s1 >> t2;
                if (tmp[s1][t2]) {
                        tmp[s1].clear();
                        tmp[s1][t2] = 1;
                        cnt[s1] = 1;
                } else {
                        cnt[s1]++;
                        tmp[s1][t2] = 1;
                }
                if (cnt[s1] >= 5) {
                        puts("PENTA KILL!");
                        return 0;
                }
        }
        puts("SAD:(");
        return 0;
}

一直wa,一直wa,总是wa,waaaaa,我崩溃了

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-22 19:57:07 | 显示全部楼层
我理解你的困扰,让我们来看看你的代码问题在哪里。

首先,你的代码没有正确地检查一名玩家是否连续击杀了五个成对的不同对手。你需要在每次杀死一个新对手时检查该玩家是否已经击杀了其他四个不同的对手。

让我们对你的代码进行一些修改:
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    unordered_map<string, unordered_set<string>> killed;
    unordered_map<string, int> killStreak;
    string killer, victim;
    
    for (int i = 0; i < n; ++i) {
        cin >> killer >> victim;
        if (killed[killer].find(victim) == killed[killer].end()) {
            // 如果击杀了一个新对手,击杀连续计数加1
            killStreak[killer]++;
            killed[killer].insert(victim);
        } else {
            // 如果击杀了相同的对手,重置击杀连续计数和击杀的对手列表
            killStreak[killer] = 1;
            killed[killer].clear();
            killed[killer].insert(victim);
        }

        if (killStreak[killer] == 5) {
            cout << "PENTA KILL!" << endl;
            return 0;
        }
    }

    cout << "SAD:(" << endl;
    return 0;
}

这个修改后的代码应该能正确地处理你所描述的问题。在每次杀死一个对手时,它都会检查是否连续击杀了五个不同的对手。如果是这样,它将输出 "PENTA KILL!" 并结束程序。如果在游戏中没有出现五杀,它将输出 "SAD:("。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-22 19:58:45 | 显示全部楼层
isdkz 发表于 2023-4-22 19:57
我理解你的困扰,让我们来看看你的代码问题在哪里。

首先,你的代码没有正确地检查一名玩家是否连续击杀 ...

很抱歉,这个代码仍然是错误的,且错误的测试点与我的代码一样。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-30 12:25:06 | 显示全部楼层
zhangjinxuan 发表于 2023-4-22 19:58
很抱歉,这个代码仍然是错误的,且错误的测试点与我的代码一样。

测试链接?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-30 14:18:56 | 显示全部楼层

测试链接不方便公布。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-30 14:31:20 | 显示全部楼层
zhangjinxuan 发表于 2023-4-30 14:18
测试链接不方便公布。

6
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-27 23:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表