[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;
    }
相关推荐
ghie90902 分钟前
基于libsvm的支持向量机在MATLAB中的实现
算法·支持向量机·matlab
Samsong1 小时前
《C++ Primer Plus》读书笔记 第二章 开始学习C++
c++·后端
Rubisco..2 小时前
牛客周赛 Round 111
数据结构·c++·算法
兮山与2 小时前
算法8.0
算法
高山上有一只小老虎2 小时前
杨辉三角的变形
java·算法
Swift社区2 小时前
LeetCode 395 - 至少有 K 个重复字符的最长子串
算法·leetcode·职场和发展
hz_zhangrl2 小时前
CCF-GESP 等级考试 2025年9月认证C++四级真题解析
开发语言·c++·算法·程序设计·gesp·c++四级·gesp2025年9月
火山灿火山2 小时前
详解AVL树旋转操作实现
数据结构·c++
少许极端2 小时前
算法奇妙屋(六)-哈希表
java·数据结构·算法·哈希算法·散列表·排序
羊羊小栈3 小时前
基于「多模态大模型 + BGE向量检索增强RAG」的新能源汽车故障诊断智能问答系统(vue+flask+AI算法)
vue.js·人工智能·算法·flask·汽车·毕业设计·大作业