PAT A1038 Recover the Smallest Number(***字符串排序)

问题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704

Note:

  1. 如果把这些数字串按字典序从小到大排序,然后顺序输出,会出现出现错误: 如{”32“,”321“}按字典序排序是”32321“,但按题意,更小的答案是”32132“。
  2. 按题意正确的贪心策略是:对数字串S1和S2,如果S1+S2 < S2+S1,则把S1放到S2前面,否则把S2放到S1前面。
  3. 特别注意学习这个cmp函数排序!!!

题意:

    给出若干可能有前导零的数字串,将它们按某个顺序拼接,使生成的数最小。

 

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 10010;
string str[maxn];
bool cmp(string a, string b){
	return a+b < b+a;//如果a+b < b+a 
}
int main(){
	int n; 
	cin >> n;
	for(int i = 0; i < n; i++)
	    cin >> str[i];
	
	sort(str, str+n, cmp);
	
	string ans; //结果字符串
	for(int i = 0; i < n; i++)
	    ans += str[i];// 将排序后的数字串进行拼接
		
	while(ans.size() > 1 && ans[0] == '0')
	    ans.erase(ans.begin());
	
	cout << ans;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用压缩感知进行图像压缩的Python代码示例,该示例使用了PyWavelets和cvxpy库: ```python import cv2 import numpy as np import pywt import cvxpy as cp import matplotlib.pyplot as plt # 读取图像并转为灰度图 img = cv2.imread('lena.png', cv2.IMREAD_GRAYSCALE) # 将图像分解为多个小波系数 coeffs = pywt.dwt2(img, 'haar') cA, (cH, cV, cD) = coeffs # 将小波系数矩阵展开为向量 cA_vec = cA.reshape(-1) cH_vec = cH.reshape(-1) cV_vec = cV.reshape(-1) cD_vec = cD.reshape(-1) # 定义测量矩阵 m = 5000 # 测量数 n = img.shape[0] * img.shape[1] # 原始信号长度 A = np.random.randn(m, n) # 生成测量结果 y = A @ np.concatenate((cA_vec, cH_vec, cV_vec, cD_vec)) # 恢复小波系数向量 x = cp.Variable(4*n) objective = cp.Minimize(cp.norm1(x)) constraints = [A @ x == y] prob = cp.Problem(objective, constraints) result = prob.solve(verbose=True) # 将恢复后的小波系数向量转为矩阵形式 cA_vec_recover = x.value[:n].reshape(cA.shape) cH_vec_recover = x.value[n:2*n].reshape(cH.shape) cV_vec_recover = x.value[2*n:3*n].reshape(cV.shape) cD_vec_recover = x.value[3*n:].reshape(cD.shape) # 合并小波系数矩阵并进行反变换 coeffs_recover = (cA_vec_recover, (cH_vec_recover, cV_vec_recover, cD_vec_recover)) img_recover = pywt.idwt2(coeffs_recover, 'haar') # 显示压缩前后的图像 plt.subplot(1, 2, 1) plt.imshow(img, cmap='gray') plt.title('Original Image') plt.subplot(1, 2, 2) plt.imshow(img_recover, cmap='gray') plt.title('Recovered Image') plt.show() ``` 注意:该示例中使用了Haar小波作为小波变换基,并且假设压缩后的小波系数矩阵是稀疏的。实际上,其他小波基和稀疏性假设也可以用于图像压缩。此外,还可以通过调整测量数和压缩比例等参数来控制压缩质量和压缩速度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值