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
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int a[],b[];
char A[],B[];
int ans[]; int main()
{
int T,iCase = ;
scanf("%d",&T);
getchar();
while(T--)
{
iCase++;
scanf("%s%s",A,B);
int n = strlen(A);
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(int i = ; i < n; i++)
{
a[A[i] - '']++;
b[B[i] - '']++;
}
int x = , y = ;
int ttt = -;
//求出首位数字
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
if(a[i] && b[j] && ((i+j)%) > ttt )
{
x = i;
y = j;
ttt = (x+y)%;
}
a[x]--;
b[y]--;
int cnt = ;
ans[cnt++] = (x+y)%;
//求出其它位结果保存到ans数组中
for(int p = ; p >= ; p--)
{
for(int i = ; i <= ; i++)
if(a[i])
{
if(i <= p)
{
int j = p-i;
int k = min(a[i],b[j]);
a[i] -= k;
b[j] -= k;
while(k--) ans[cnt++] = p;
}
int j = + p - i;
if(j > )continue;
int k = min(a[i],b[j]);
a[i] -= k;
b[j] -= k;
while(k--) ans[cnt++] = p;
}
}
printf("Case #%d: ",iCase);
int s = ;
while(s < cnt- && ans[s] == )s++;
for(int i = s; i < cnt; i++) printf("%d",ans[i]);
printf("\n");
}
return ;
}

贪心

Kia's Calculation(HDU 4267)的更多相关文章

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

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

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

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

  3. 2道acm编程题(2014):1.编写一个浏览器输入输出(hdu acm1088);2.encoding(hdu1020)

    //1088(参考博客:http://blog.csdn.net/libin56842/article/details/8950688)//1.编写一个浏览器输入输出(hdu acm1088)://思 ...

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

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

  5. K - Kia's Calculation(贪心)

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

  6. 2012年长春网络赛(hdu命题)

    为迎接9月14号hdu命题的长春网络赛 ACM弱校的弱菜,苦逼的在机房(感谢有你)呻吟几声: 1.对于本次网络赛,本校一共6名正式队员,训练靠的是完全的自主学习意识 2.对于网络赛的群殴模式,想竞争现 ...

  7. Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b ...

  8. 2013 多校联合 F Magic Ball Game (hdu 4605)

    http://acm.hdu.edu.cn/showproblem.php?pid=4605 Magic Ball Game Time Limit: 10000/5000 MS (Java/Other ...

  9. (多线程dp)Matrix (hdu 2686)

    http://acm.hdu.edu.cn/showproblem.php?pid=2686     Problem Description Yifenfei very like play a num ...

随机推荐

  1. webui layout like desktop rich client

    similarity similarlike desktop js frameworklike extj js frameworklike rich client js frameworkjs lay ...

  2. OpenJudge 2790 迷宫

    1.链接地址: http://bailian.openjudge.cn/practice/2790/ 2.题目: 总时间限制: 3000ms 内存限制: 65536kB 描述 一天Extense在森林 ...

  3. Hash函数的安全性

    我们为了保证消息的完整性,引进了散列函数,那么散列函数会对安全正造成什么影响呢?这是需要好好研究一番的问题. 三个概念: 1.如果y<>x,且h(x)=h(y),则称为碰撞. 2.对于给定 ...

  4. ubuntu desktop 开机 连接网络

    参考   http://linux.net527.cn/Ubuntu/Ubuntuanzhuangyuyingyong/2490.html

  5. if参数小结

    条件表达式 if [ -f  file ]    如果文件存在 if [ -d ...   ]    如果目录存在 if [ -s file  ]    如果文件存在且非空 if [ -r file  ...

  6. Google Web Designer 测试

    这东东完全就是一个flash啊,简单测试,感觉就是个做HTML5动画的..不过暂时是beta版的, 官方安装版的半天打不开,这边有个绿色版的,需要的童鞋可以这里下载:百度网盘

  7. jquery 中的 this 和 $(this)

    this,表示当前的上下文对象是一个html对象,可以调用html对象所拥有的属性,方法 $(this),代表的上下文对象是一个jquery的上下文对象,可以调用jquery的方法和属性值. 亦即: ...

  8. xml技术DTD约束定义

    XML约束 在XML技术中,可以编写一个文档来约束一个xml文档的书写规范,这称之为XML约束为什么需要XML约束? class.xml <stu><面积>?人怎么会有面积元素 ...

  9. php缓存相关

    在php运行期间,php引擎要对php源码进行处理,(词法分析,语法分析等)然后生成opcode. 然后再运行.在这个阶段可以把opcode缓存起来,当下次需要运行这段程序的时候,就避免了再次 进行词 ...

  10. nginx 反向代理 odoo, 速度提升

    1: sudo apt-get  install nginx 2:  add server: 在/etc/nginx/sites-enabled 中增加一个可用的server. 文件如下图所示 3:重 ...