C++笔试强训day39

目录

1.神奇的字母(二)

2.字符编码

3.最少的完全平方数


1.神奇的字母(二)

链接icon-default.png?t=N7T8https://ac.nowcoder.com/acm/problem/205832



看输出描述即可知输出次数最多的那个字母即可。

哈希表直接秒了:

#include <iostream>
#include <string>
using namespace std;
int cnt[26];
int m_cnt, keyi;
int main() {
	string s;
	while (getline(cin, s))
	{
		for (auto c : s)
			if (c != ' ')
				cnt[c - 'a']++;
		for (int i = 0; i < 26; ++i)
		{
			if (cnt[i] > m_cnt)
			{
				m_cnt = cnt[i];
				keyi = i;
			}
		}
	}
	char ret = 'a' + keyi;
	cout << ret << endl;
	return 0;
}

2.字符编码

链接icon-default.png?t=N7T8https://www.nowcoder.com/practice/c471efdbd33a4a979539a91170c9f1cb?tpId=128&tqId=33774&ru=/exam/oj

即为哈夫曼编码:与该篇中的模版题极为相似-》模版icon-default.png?t=N7T8https://blog.csdn.net/cy18779588218/article/details/139304233?spm=1001.2014.3001.5501

#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#define int long long
using namespace std;

signed main() {
	string s;
	while (cin >> s) {
		int ret = 0;
		int cnt[250] = { 0 };
		for (auto c : s)
			cnt[c]++;

		priority_queue<int, vector<int>, greater<int>> pq;
		for (int n : cnt)
			if (n != 0)
				pq.push(n);

		while (pq.size() != 1) {
			int top1 = pq.top();
			pq.pop();
			int top2 = pq.top();
			pq.pop();
			ret += (top1 + top2);
			pq.push(top1 + top2);
		}
		cout << ret << endl;
	}
	return 0;
}

3.最少的完全平方数

链接icon-default.png?t=N7T8https://www.nowcoder.com/practice/4b2f5d4c00f44a92845bdad633965c04?tpId=230&tqId=40431&ru=/exam/oj

类似完全背包题目:

搞清楚他的状态表示{

        dp[i][j] : 从前i个数中挑选,总和恰好为j时,需要挑选的最少个数

}就很好做题了。(不过要先了解过完全背包

注意留意他的初始化,因为是去最小值,所以可以直接将dp表初始化为INT_MAX,然后初始化一下填表需要用到的位置:

#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;

int arr[110];
int dp[110][10010];
int n;

int main()
{
    cin >> n;
    for(int i = 1; i <= 100; ++i)
        arr[i] = i * i;

    int r = sqrt(n);
    memset(dp, 0x3f, sizeof dp);
    for(int i = 1; i <= r; ++i)
        dp[i][0] = 0;

    for(int i = 1; i <= r; ++i)
    {
        for(int j = 1; j <= n; ++j)
        {
            dp[i][j] = dp[i - 1][j];
            if(j >= arr[i])
                dp[i][j] = min(dp[i - 1][j], dp[i][j - arr[i]] + 1);
        }
    }
    cout << dp[r][n] << endl;
    return 0;
}

当然,也可以进行空间优化。

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-06-05 21:28:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-05 21:28:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-05 21:28:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-05 21:28:02       20 阅读

热门阅读

  1. 【FPGA约束】如何对fpga进行io约束

    2024-06-05 21:28:02       9 阅读
  2. spring-Bean的作用域

    2024-06-05 21:28:02       8 阅读
  3. Flutter 中的 Table 小部件:全面指南

    2024-06-05 21:28:02       7 阅读
  4. pytest-playwright 插件的使用

    2024-06-05 21:28:02       9 阅读
  5. wpf窗体背景添加径向渐变效果实现

    2024-06-05 21:28:02       6 阅读
  6. 【LeetCode 1】两数之和

    2024-06-05 21:28:02       8 阅读
  7. RUST运算符重载

    2024-06-05 21:28:02       5 阅读
  8. 【linux自动化实践】linux shell 脚本 替换某文本

    2024-06-05 21:28:02       9 阅读