[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;
    }
相关推荐
枕星而眠几秒前
C++ 面向对象核心机制深度解析:多态性、虚函数、虚继承与 final 类
运维·开发语言·c++·后端
小蒋学算法16 分钟前
算法-乘法表中第K小的数-二分
数据结构·算法
智者知已应修善业25 分钟前
【51单片机8个LED,已经使用了D1D2,怎么样在不动D1D2的前提下实现D6~D8的流水灯】2024-1-19
c++·经验分享·笔记·算法·51单片机
Evand J26 分钟前
【MATLAB例程】自适应渐消扩展卡尔曼滤波(AFEKF)三维雷达目标跟踪|效果已调优,附下载链接和运行结果,代码直接运行即可
开发语言·算法·matlab·目标跟踪·卡尔曼滤波·自适应滤波·代码定制
坚果派·白晓明27 分钟前
鸿蒙PC适配实战:simdjson 三方库移植攻略与 AtomCode Skills 提效之道
c++·harmonyos·三方库·skills·atomcode·c/c++三方库·c/c++三方库适配
爱装代码的小瓶子27 分钟前
3. 设计buffer模块
linux·服务器·开发语言·c++·php
郝学胜-神的一滴28 分钟前
Qt 高级开发 027: QTabWidget自定义样式表美化实战
开发语言·c++·qt·程序人生·软件构建·用户界面
双河子思29 分钟前
《代码整洁之道》——读书笔记(持续更新)
开发语言·c++·c#
郝学胜-神的一滴1 小时前
Qt 高级开发 026:QTabWidget御道,从筑基到化境
开发语言·c++·qt·程序人生·软件构建·用户界面
插件开发1 小时前
矢量路径运算如何选GPU技术?——适用算法对比及OpenGL/Direct3D/CUDA选型指南
算法·3d