[Algorithm][综合训练][字符编码][最少的完全平方数][游游的字母串]详细讲解

目录


1.字符编码

1.题目链接


2.算法原理详解 && 代码实现

  • 解法:给一个字符串进行二进制编码,使得编码后的字符串长度最短 --> 哈夫曼编码

    cpp 复制代码
    #include <iostream>
    #include <string>
    #include <vector>
    #include<queue>
    using namespace std;
    
    int main()
    {
        string str;
        while(cin >> str)
        {
            // 1.统计每个字符的频次
            int hash[300] = { 0 };
            for(const auto& ch : str)
            {
                hash[ch]++;
            }
    
            // 2.将所有的频次放入堆中
            priority_queue<int, vector<int>, greater<>> heap;
            for(int i = 0; i < 300; i++)
            {
                if(hash[i])
                {
                    heap.push(hash[i]);
                }
            }
    
            // 3.哈夫曼编码
            int ret = 0;
            while(heap.size() > 1)
            {
                int x1 = heap.top();
                heap.pop();
                int x2 = heap.top();
                heap.pop();
    
                ret += x1 + x2;
                heap.push(x1 + x2);
            }
    
            cout << ret << endl;
        }
    
        return 0;
    }

2.最少的完全平方数

1.题目链接


2.算法原理详解 && 代码实现

  • 思路:从一些数里面选,每个数都可以选无穷多次,在限定条件下,达到目的

  • 解法 :完全背包 -> 空间优化版本

    • 状态表示dp[i][j]:从前i割数中挑选,总和恰好为j时,最少挑出来几个数

    • 状态转移方程

    • 初始化

    • 返回值dp[sqrt(n)][n]

    cpp 复制代码
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    const int N = 1e4 + 10;
    
    int main()
    {
    	int n = 0;
    	cin >> n;
    	
    	int dp[N];
    	memset(dp, 0x3f, sizeof dp);
    	dp[0] = 0;
    
    	for(int i = 1; i * i <= n; i++)
    	{
    		for(int j = i * i; j <= n; j++)
    		{
    			dp[j] = min(dp[j], dp[j - i * i] + 1);
    		}
    	}
    
    	cout << dp[n] << endl;
    
    	return 0;
    }

3.游游的字母串

1.题目链接


2.算法思路详解 && 代码实现

  • 解法暴力枚举所有可能变成的字符情况

    • 如何求变化次数min(abs(a - z), 26 - abs(a - z))
    cpp 复制代码
    #include <iostream>
    #include <cmath>
    #include <string>
    using namespace std;
    
    int main()
    {
        string str;
        cin >> str;
        
        int ret = 0x3f3f3f3f;
        for(char ch = 'a'; ch <= 'z'; ch++) // 枚举变成什么字符
        {
            int sum = 0;
            for(auto x : str)
            {
                sum += min(abs(x - ch), 26 - abs(x - ch));
            }
            ret = min(ret, sum);
        }
        
        cout << ret << endl;
        
        return 0;
    }
相关推荐
Non importa5 分钟前
【初阶数据结构】树——二叉树(上)
c语言·数据结构·学习·算法
刘卜卜&嵌入式1 小时前
C++_设计模式_观察者模式(Observer Pattern)
c++·观察者模式·设计模式
h汉堡2 小时前
C++入门基础
开发语言·c++·学习
XINVRY-FPGA2 小时前
XCZU7EG‑L1FFVC1156I 赛灵思XilinxFPGA ZynqUltraScale+ MPSoC EG
c++·嵌入式硬件·阿里云·fpga开发·云计算·fpga·pcb工艺
Tech Synapse2 小时前
基于Surprise和Flask构建个性化电影推荐系统:从算法到全栈实现
python·算法·flask·协同过滤算法
終不似少年遊*2 小时前
国产之光DeepSeek架构理解与应用分析04
人工智能·python·深度学习·算法·大模型·ds
天天扭码2 小时前
一分钟解决 | 高频面试算法题——最大子数组之和
前端·算法·面试
杰杰批2 小时前
力扣热题100——矩阵
算法·leetcode·矩阵
明月看潮生2 小时前
青少年编程与数学 02-016 Python数据结构与算法 28课题、图像处理算法
图像处理·python·算法·青少年编程·编程与数学
_GR3 小时前
2025年蓝桥杯第十六届C&C++大学B组真题及代码
c语言·数据结构·c++·算法·贪心算法·蓝桥杯·动态规划