#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没有关系。