HDU 4726 贪心

#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn = 1E6 + 10;
char str1[maxn], str2[maxn], ans[maxn];
int a[10], b[10], T, kase;
vector<pair<int, int>>W[10], K[10];//W without 0,K within 0;
void init()
{
	for (int i = 0; i <= 9; i++)
		for (int j = 0; j <= 9; j++)
		{
			if (i != 0 && j != 0)
				W[(i + j) % 10].push_back(make_pair(i, j));
			K[(i + j) % 10].push_back(make_pair(i, j));
		}
}
int dfs(int k)
{
	vector<pair<int, int>> *p = (k ? K : W);
	for (int i = 9; i >= 0; i--)
		for (int j = 0; j < p[i].size(); j++)
			if (a[p[i][j].first] > 0 && b[p[i][j].second] > 0)
			{
				a[p[i][j].first]--;
				b[p[i][j].second]--;
				return i;
			}
	return 0;
}
int main(int argc, char const *argv[])
{
	init();
	scanf("%d", &T);
	while (T--)
	{
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		memset(ans, 0, sizeof(ans));
		scanf("%s%s", str1, str2);
		int len = strlen(str1);
		for (int i = 0; i < len; i++)
			a[str1[i] - '0']++, b[str2[i] - '0']++;
		if (len == 1) printf("Case #%d: %d\n", ++kase, (str1[0] - '0' + str2[0] - '0') % 10);
		else
		{
			ans[0] = dfs(0) + '0';
			if (ans[0] == '0') {printf("Case #%d: 0\n", ++kase); continue;}
			for (int i = 1; i < len; i++)
				ans[i] = dfs(i) + '0';
			ans[len] = 0;
			printf("Case #%d: %s\n", ++kase, ans);
		}
	}
	return 0;
}


英语阅读题,Both A and B will have same number of digits, which is no larger than 106, and without leading zeros.

是A和B有10^6大还是数字有10^6个? 数字啊,修饰have same number of digits 啊,搞清楚这个就好了。

尽量先挑大的。具体挑了0-9还是1-8没有关系。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值