GJX的表白题解

简单的字符串模拟 用哈希表存下每个字符出现的次数,最后逐个比较即可
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>

using namespace std;

string a, b;
int n;
int t;

int main(void)
{
    scanf("%d", &t);
    getchar();
    while(t -- )
    {
        unordered_map<char, int> h1, h2;

        getline(cin, a);
        getline(cin, b);
        for(int i = 0 ; i < a.size() ; i ++ ) h1[a[i]] ++, h2[b[i]] ++ ;

        if(h1.size() != h2.size()) printf("GJX is unhappy\n");
        else
        {
            bool success = true;
            for(auto item : h1)
            {
                if(!h2.count(item.first))
                {
                    success = false;
                    break;
                }else
                {
                    if(h1[item.first] != h2[item.first])
                    {
                        success = false;
                        break;
                    }
                }
            }
            if(success) puts("GJX is happy");
            else puts("GJX is unhappy");
        }
    }
    return 0;
}

全部评论
这样每次开一个unordered_map是不是很慢?
7 回复
分享
发布于 2020-12-08 12:53

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务