Day 21
目录
- 1.AB32 【模板】哈夫曼编码
- 1.1 解析
- 1.2 代码
- 2.DP36 abb
- 2.1解析
- 2.2 代码
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;
}