贪心 HDOJ 4726 Kia's Calculation

时间:2024-01-03 21:31:38

题目传送门

 /*
这题交给队友做,做了一个多小时,全排列,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 ;
}