555. 相同字母异序词 ((unordered_set 哈希向量(数组)))

本文介绍了如何在C++中利用unordered_set处理字符串和数组,特别是面对不能直接哈希数组的情况,可以通过自定义哈希函数来实现。提到了ACwing平台上的相关题目和博客资源。
摘要由CSDN通过智能技术生成

题目链接 https://www.acwing.com/problem/content/557/
c++ 可以哈希字符串,有内置函数,但是他不能哈希向量,即不能哈希数组,但是有一个接口,可以自己新定义一个哈希函数 https://www.acwing.com/blog/content/9/

unordered_set 就是哈希的类型
需要引入头文件#include<unordered_set>

#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<unordered_set>
using namespace std;
typedef unsigned long long ull;
const int p  = 131;

ull get_hash(int s[]){
    ull res=  0;
    for(int i =0;i<26;i++){
        res = res*p+ s[i];
    }
    return res;
}
int main(){
    int T;
    cin >>T;

    for (int cases = 1;cases<=T;cases ++){
        int n;
        string a,b;
        cin>>n;
        cin>>a>>b;

        unordered_set<ull> S;

        int s[26];
        for(int i=0;i<n;i++){
            for(int j = i;j<n;j++){
                memset(s,0,sizeof s);
               for(int k =i;k<=j;k++)      s[b[k]- 'A']++;  //这个B是什么鬼
                S.insert(get_hash(s));    //直接哈希一个向量(哈希一个数组)

            }
        }
        int res = 0;
        for(int i =0;i<n;i++){
            for(int j = i;j<n;j++){
                memset(s,0,sizeof s);
                for(int k = i;k<=j;k++) s[a[k]- 'A']++;
                if(S.count(get_hash(s))) res++;

            }
        }
        printf("Case #%d: %d\n",cases,res);

    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值