[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;
    }
相关推荐
2601_9622186110 小时前
C++中decltype关键字的实现
开发语言·c++
Discipline102910 小时前
尼泊尔河流流量:天气驱动的预测与高流量风险
人工智能·算法
繁华的地方不一定留下你的脚印10 小时前
C++ std::variant 与 std::visit:安全保存多种类型,写清每个处理分支
开发语言·c++
XuCoder10 小时前
改一个数,右边全得重算,这题怎么扛住两万次查询
算法
小羊没烦恼!10 小时前
在Scrum中实施敏捷建模
java·开发语言·windows·算法·c#
程序员老陆10 小时前
WIN32_LEAN_AND_MEAN:Windows 头文件里的“精简编译开关”
c++·windows
weixin_3077791310 小时前
基于睿擎工业开发平台的预训练视觉模型轻量化适配与低代码部署优化
开发语言·算法
hold?fish:palm11 小时前
45 二叉树的右视图
开发语言·c++·算法
粤鼎恒业12 小时前
惠州工厂电子料回收厂家推荐:从交接单反推筛选标准
大数据·数据库·算法·硬件架构·硬件工程·pcb工艺·材料工程
ShineWinsu12 小时前
对于Redis:Redis的认识以及分布式系统的解析
linux·数据库·c++·redis·缓存·消息队列·分布式架构