Kia's Calculation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3291    Accepted Submission(s): 703

Problem Description
Doctor
Ghee is teaching Kia how to calculate the sum of two integers. But Kia
is so careless and alway forget to carry a number when the sum of two
digits exceeds 9. For example, when she calculates 4567+5789, she will
get 9246, and for 1234+9876, she will get 0. Ghee is angry about this,
and makes a hard problem for her to solve:
Now Kia has two integers A
and B, she can shuffle the digits in each number as she like, but
leading zeros are not allowed. That is to say, for A = 11024, she can
rearrange the number as 10124, or 41102, or many other, but 02411 is not
allowed.
After she shuffles A and B, she will add them together, in her own way. And what will be the maximum possible sum of A "+" B ?
 
Input
The rst line has a number T (T <= 25) , indicating the number of test cases.
For each test case there are two lines. First line has the number A, and the second line has the number B.
Both A and B will have same number of digits, which is no larger than 106, and without leading zeros.
 
Output
For test case X, output "Case #X: " first, then output the maximum possible sum without leading zeros.
 
Sample Input
1
5958
3036
 
Sample Output
Case #1: 8984
 
Source
 
题意:给出两个长度不超过 10^6 的数字串,数字串可以打乱后随机组合,但是打乱重组后不能有前导 0,将这两个数字串相加,相加的规则是每一位相加,不实现进位.问能够得到最大的结果串是多少?
题解:贪心求解,从高位到低位,每次贪心选择相加起来最大的数字,最高位要单独处理,因为最高位要求被加数,加数,结果都不能为 0.然后在贪心的过程中不能去枚举加数和被加数,只能枚举结果,不然会超时. 当位数只有1,加数或者被加数中间有一个为0时单独处理.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define LL long long
using namespace std;
const int N = ;
char str1[N],str2[N];
int num1[],num2[];
int res[N];
int main()
{
int tcase,t = ;
scanf("%d",&tcase);
while(tcase--)
{ scanf("%s%s",str1,str2);
if(strcmp(str1,"")==){
printf("Case #%d: ",t++);
printf("%s\n",str2);
continue;
}
if(strcmp(str2,"")==){
printf("Case #%d: ",t++);
printf("%s\n",str1);
continue;
}
int len = strlen(str1);
memset(num1,,sizeof(num1));
memset(num2,,sizeof(num2));
for(int i=; i<len; i++)
{
num1[str1[i]-'']++;
num2[str2[i]-'']++;
}
int high = -,x,y;
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
if(num1[i]&&num2[j]&&high<(i+j)%)
{
x = i;
y = j;
high = (i+j)%;
}
}
}
num1[x]--;
num2[y]--;
int cnt = ,zero = ;
res[cnt++] = high;
if(high==) zero++;
printf("Case #%d: ",t++);
if(zero){
printf("0\n");
continue;
}
for(int l=; l<len; l++)
{
/*
TLE
for(int i=0; i<=9; i++)
{
for(int j=0; j<=9; j++)
{
if(num1[i]&&num2[j]&&MAX<(i+j)%10)
{
x = i;
y = j;
MAX = (i+j)%10;
}
}
}*/
bool flag = true;
for(int i=;i>=&&flag;i--){
for(int j=;j<=&&flag;j++){
if(i-j<&&num1[j]&&num2[i-j+]){
num1[j]--;
num2[i-j+]--;
res[cnt++] = i;
flag = false;
}else if(i-j>=&&num1[j]&&num2[i-j]){
num1[j]--;
num2[i-j]--;
res[cnt++] = i;
flag = false;
}
}
} }
for(int i=;i<cnt;i++){
printf("%d",res[i]);
}
printf("\n");
}
return ;
}
 

hdu 4726(贪心)的更多相关文章

  1. HDU 4726 Kia's Calculation(贪心构造)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4726 题意:给出两个n位的数字,均无前缀0.重新排列两个数字中的各个数,重新排列后也无前缀0.得到的两 ...

  2. HDU 4726 Kia's Calculation (贪心算法)

    Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...

  3. HDU 4726 Kia's Calculation(贪心)

    Kia's Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)

    DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so carel ...

  5. Hdu 5289-Assignment 贪心,ST表

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...

  6. hdu 4803 贪心/思维题

    http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...

  7. hdu 1735(贪心) 统计字数

    戳我穿越:http://acm.hdu.edu.cn/showproblem.php?pid=1735 对于贪心,二分,枚举等基础一定要掌握的很牢,要一步一个脚印走踏实 这是道贪心的题目,要有贪心的意 ...

  8. hdu 4974 贪心

    http://acm.hdu.edu.cn/showproblem.php?pid=4974 n个人进行选秀,有一个人做裁判,每次有两人进行对决,裁判可以选择为两人打分,可以同时加上1分,或者单独为一 ...

  9. hdu 4982 贪心构造序列

    http://acm.hdu.edu.cn/showproblem.php?pid=4982 给定n和k,求一个包含k个不相同正整数的集合,要求元素之和为n,并且其中k-1的元素的和为完全平方数 枚举 ...

随机推荐

  1. K Closest Numbers In Sorted Array

    Given a target number, a non-negative integer k and an integer array A sorted in ascending order, fi ...

  2. 【Python简介】

    一.Python的简介 1.什么是python? Python(发音:[ 'paiθ(ə)n; (US) 'paiθɔn ]),是一种面向对象的解释性的计算机程序设计语言,也是一种功能强大而完善的通用 ...

  3. 开放接口/RESTful/Api服务的设计和安全方案

    总体思路 这个涉及到两个方面问题:一个是接口访问认证问题,主要解决谁可以使用接口(用户登录验证.来路验证)一个是数据数据传输安全,主要解决接口数据被监听(HTTPS安全传输.敏感内容加密.数字签名) ...

  4. HDU 4372 Count the Buildings——第一类斯特林数

    题目大意:n幢楼,从左边能看见f幢楼,右边能看见b幢楼 楼高是1~n的排列. 问楼的可能情况 把握看到楼的本质! 最高的一定能看见! 计数问题要向组合数学或者dp靠拢.但是这个题询问又很多,难以dp ...

  5. Canny边缘检测原理及C#程序实现

    http://blog.csdn.net/yjz_uestc/article/details/6664937 Canny边缘检测是被公认的检测效果最好的边缘检测方法,是由John F. Canny于1 ...

  6. 洛谷P1890 gcd区间

    题目描述 给定一行n个正整数a[1]..a[n]. m次询问,每次询问给定一个区间[L,R],输出a[L]..a[R]的最大公因数. 输入输出格式 输入格式: 第一行两个整数n,m. 第二行n个整数表 ...

  7. 手脱ASPack v2.12变形壳2

    1.PEID载入 ASPack v2.12 2.载入OD,跟之前帖子的入口特征相同,都是一个pushad,但是请不要怀疑这是同一个壳,绝对不是,pushad下一行ESP定律下硬件断点,然后shift+ ...

  8. java中16进制转换10进制

    java中16进制转换10进制 public static void main(String[] args) { String str = "04e1"; String myStr ...

  9. google的几道面试题

    Question1:如何判断两个矩形是否有重叠区域? Answer(java): public boolean checkCross(Rectangle m, Rectangle n) { //求出m ...

  10. 解决问题Can’t connect to local MySQL server through socket

    不幸遇到MySQL出现ERROR 2002 (HY000): Can’t connect to local mysql server through socket ‘/tmp/mysql.sock’错 ...