贪心 HDOJ 4726 Kia's Calculation
/*
这题交给队友做,做了一个多小时,全排列,RE数组越界,赛后发现读题读错了,囧!
贪心:先确定最高位的数字,然后用贪心的方法,越高位数字越大 注意:1. Both A and B will have same number of digits 两个数字位数相同
2. which is no larger than 10^6 不是大小,而是长度不超过1e6
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
using namespace std; const int MAXN = 1e6 + ;
const int INF = 0x3f3f3f3f;
char s1[MAXN], s2[MAXN]; int main(void) //HDOJ 4726 Kia's Calculation
{
//freopen ("K.in", "r", stdin); int t, cas = ;
int cnt1[], cnt2[], cnt3[]; scanf ("%d", &t);
while (t--)
{
scanf ("%s", &s1);
scanf ("%s", &s2); printf ("Case #%d: ", ++cas); int len = strlen (s1);
if (strcmp (s1, "") == )
{
printf ("%s\n", s2); continue;
}
else if (strcmp (s2, "") == )
{
printf ("%s\n", s1); continue;
} memset (cnt1, , sizeof (cnt1));
memset (cnt2, , sizeof (cnt2));
memset (cnt3, , sizeof (cnt3)); for (int i=; i<len; ++i)
{
cnt1[s1[i]-'']++; cnt2[s2[i]-'']++;
} int ii = , jj = , mx = -;
for (int i=; i<=; ++i)
{
if (cnt1[i] == ) continue;
for (int j=; j<=; ++j)
{
if (cnt2[j] == ) continue;
int tmp = (i + j) % ;
if (tmp > mx)
{
mx = tmp; ii = i; jj = j;
}
}
}
cnt1[ii]--; cnt2[jj]--;
if (!mx)
{
puts (""); continue;
} for (int i=; i>=; --i)
{
for (int j=; j<=; ++j)
{
for (int k=; k<=; ++k)
{
if ((j+k)%==i && cnt1[j] && cnt2[k])
{
int tmp = min (cnt1[j], cnt2[k]);
cnt1[j] -= tmp; cnt2[k] -= tmp;
cnt3[i] += tmp;
}
}
}
} printf ("%d", mx);
for (int i=; i>=; --i)
{
for (int j=; j<cnt3[i]; ++j) printf ("%d", i);
}
puts ("");
} return ;
}
贪心 HDOJ 4726 Kia's Calculation的更多相关文章
- HDU 4726 Kia's Calculation (贪心算法)
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 4726 Kia's Calculation(贪心)
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)
DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so carel ...
- HDU 4726 Kia's Calculation(贪心构造)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4726 题意:给出两个n位的数字,均无前缀0.重新排列两个数字中的各个数,重新排列后也无前缀0.得到的两 ...
- hdu 4726 Kia's Calculation
思路:刚开始想复杂了. 看解题报告后才知道这题挺简单的,看来还是要多训练啊!!! 单独处理首位的数字,不能为0.其他的就好处理了,从大到小依次找下去就可以了…… 代码如下: #include<i ...
- K - Kia's Calculation(贪心)
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- K - Kia's Calculation (贪心)
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Kia's Calculation hdu4726
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU-4726 Kia's Calculation 贪心
题目链接:http://acm.hdu.edu.cn/userstatus.php?user=zhsl 题意:给两个大数,他们之间的加法法则每位相加不进位.现在可以对两个大数的每位重新排序,但是首位不 ...
随机推荐
- 用poi框架进行批量导入导出实例
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能.我们这里使用poi对数据库中的数据进行批量导出,以及 ...
- Leetcode 之Validate Binary Search Tree(53)
判断是否是有效的二叉搜索树,即左子树的值小于根结点,右子树的值大于根结点.可以采用递归的方式来完成,递归时如何 传递有效的参数与根结点进行比较,是此题的难点. bool isValidBST(Tree ...
- SVN安装与配置 SVN整合MyEclipse
SVN安装: 1.安装服务器 ######### 安装文件:SVN服务器############### # http://www.collab.net/downloads/subversion # C ...
- 03-VTK基础概念(2)
3.3 光照 剧场里有各式各样的灯光,三维渲染场景中也一样,可以有多个光照存在.光照和相机是三维渲染场景必备的因素,如果没有指定(像3.1.1_RenderCylinder例子,我们没有给Render ...
- 18个网站SEO建议
第一位专家是Autotrader公司的搜索市场经理Dewi Nawasari,她认为SEO就是优化网站,以吸引你的目标客户的过程.她的建议如下: 1.创建良好的引导链接 要把用户的使用过程尽量的简化, ...
- 【SpringMVC】SpringMVC系列11之Restful的CRUD
11.Restful的CRUD 11.1.需求 11.2.POST转化为PUT.DELETE的fileter 11.3.查询所有 11.4.添加 11.5.删除 优雅的 REST 风格的资 ...
- 2.7 编程之美--最大公约数的3种解法[efficient method to solve gcd problem]
[本文链接] http://www.cnblogs.com/hellogiser/p/efficient-method-to-solve-gcd-problem.html [题目] 求两个正整数的最大 ...
- Java for LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- codeforces B. Simple Molecules 解题报告
题目链接:http://codeforces.com/problemset/problem/344/B 题目意思:这句话是解题的关键: The number of bonds of an atom i ...
- 配置SecureCRT连接本地虚拟机中的Linux系统
转自:http://www.pythoner.com/196.html 由于平时公司开发时都是使用SecureCRT连接的Linux服务器,所以也想使用SecureCRT在自己电脑上连接本地虚拟机中的 ...