Day 21

1.AB32 【模板】哈夫曼编码

AB32 【模板】哈夫曼编码
堆、哈希、数组

1.1 解析

在这里插入图片描述

1.2 代码

#include <functional>
#include <iostream>
using namespace std;
#include <queue>
#include <vector>

long long n,x,ret;
int main()
{
    cin>>n;
    priority_queue<long long,vector<long long>,greater<long long>> heap;//创建一个小根堆
    for(int i=0;i<n;i++)
    {
        cin>>x;
        heap.push(x);
    }
    //构建最优二叉树
    while(heap.size()>1)
    {
        long long a=heap.top();heap.pop();
        long long b=heap.top();heap.pop();
        ret+=a+b;
        heap.push(a+b);
    }
    cout<<ret<<endl;
    return 0;
}

2.DP36 abb

DP36 abb
动态规划、哈希

2.1解析

在这里插入图片描述

2.2 代码

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

typedef long long LL;
const int N=2e5+10;
LL f[26],g[26],dp[N];
int main()
{
    int n;
    string arr;
    cin>>n>>arr;
    LL ret=0;
    for(int i=0;i<n;i++)
    {
        int x=arr[i]-'a';
        dp[i]=f[x];
        ret+=dp[i];

        f[x]=f[x]+i-g[x];
        g[x]+=1;
    }
    cout<<ret<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值