商品总类

题目 商品种类

image-88d02237

思路分析

只要看某个串是否出现过 用unordered_set

注意不能直接拼接 ab c 和 a bc是不一样的 拼接时加个空格

代码实现

哈希表 17 ms

#include<bits/stdc++.h>
using namespace std;

unordered_set<string> hashtb;

int main()
{
    int n;
    cin>>n;
    while(n--){
        string a,b;
        cin>>a>>b;
        hashtb.insert(a+' '+b);
    }
    cout<<hashtb.size()<<endl;
    return 0;
}

set 24ms

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'

typedef pair<string,string> PSS;
set<PSS> hx;

int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int n;cin>>n;
    while(n--){
        string a,b;cin>>a>>b;
        hx.insert({a,b});
    }
    cout<<hx.size();
    return 0;
}

同类题型

视频讲解


⬅️ 哈希表相关问题 🏠 00-刷题理模型 ➡️ 收集卡牌