对于每个角色有四种可能属性,a:10 b:01 c:11 d:00。
思路:构造的情况要满足a1+c1==b2+c2,然后可以枚举第一层用到几个1,第二层枚举其中来自a的个数,然后移项推公式判断。。。。
int main()//a:10 b:01 c:11 d:00 枚举:外层枚举第一次有几个人生效,内层枚举其中来自a的个数 然后推变量。。
{
int n;
string s1, s2;
while (cin >> n >> s1 >> s2)
{
vector<int> t1, t2, t3, t4;
int n1 = 0, n2 = 0, n3 = 0, n4 = 0;
f(i, 0, n - 1)
{
if (s1[i] == '1'&&s2[i] == '0')n1++, t1.emplace_back(i);
else if (s1[i] == '0'&&s2[i] == '1')n2++, t2.emplace_back(i);
else if (s1[i] == '1'&&s2[i] == '1')n3++, t3.emplace_back(i);
else n4++, t4.emplace_back(i);
}
int fg = 0;
int a = 0, c = 0, b = 0;
f(i, 0, min(n / 2, n1 + n3))
{
for (int j = 0;j <= i && j <= n1;j++)
{
int a1 = j, c1 = i - j, c2 = n3 - c1;
int imgb2 = a1 + c1 - c2;
if (imgb2 >= 0 && imgb2 <= n2)
{
a = a1, c = c1, b = n2 - imgb2;
if (n / 2 - a - c - b >= 0&&n/2-a-b-c<=n4)
{
fg = 1;
goto he;
}
}
}
}
he:
if (!fg) { puts("-1");continue; }
int re = n / 2 - a - c - b;
f(i, 0, t1.size() - 1)
{
if (a == 0)break;
printf("%d ", t1[i] + 1), a--;
}
f(i, 0, t3.size() - 1)
{
if (c == 0)break;
printf("%d ", t3[i] + 1), c--;
}
f(i, 0, t2.size() - 1)
{
if (b == 0)break;
printf("%d ", t2[i] + 1), b--;
}
f(i, 0, t4.size() - 1)
{
if (re == 0)break;
printf("%d ", t4[i] + 1), re--;
}puts("");
}
return 0;
}