三元数异或(秋季每日一题 4)
原创
©著作权归作者所有:来自51CTO博客作者sweetheart7_7的原创作品,请联系作者获取转载授权,否则将追究法律责任
如果一个整数,其各个数位不包含 
例如,
给定一个可能很长的三元数
,其首位数字(最左边那位)保证为
,其他位数字为
或 
我们规定,两个长度为
的三元数
和
可以通过三元异或运算
得到另一个长度为
的三元数
。
设
分别表示
的第
位的数字,则
。
例如,
。
你的任务是对于每个给定的
,求出可以满足
,并且
尽可能小的
。
注意,
都必须是
位三元数,且不含前导
。
输入格式
第一行包含整数
,表示共有 
每组数据第一行包含整数
,表示 
第二行包含一个长度为
,首位为
的三元数
。
输出格式
每组数据输出占两行,分别包含数
和数
。
如果答案不唯一,则输出任意合理答案均可。
数据范围


同一测试点所有
的和不超过
。
输入样例:
4
5
22222
5
21211
1
2
9
220222021
输出样例:
11111
11111
11000
10211
1
1
110111011
110111010
#include<iostream>
#include<cstring>
using namespace std;
const int N = 50010;
typedef long long LL;
int n;
char s[N], a[N], b[N];
int main(){
int t;
scanf("%d", &t);
while(t--){
scanf("%d%s", &n, s);
char c1, c2;
bool flag = false;
for(int i = 0; i < n; i++){
if(s[i] == '0'){
c1 = c2 = '0';
}else if(s[i] == '1'){
if(flag) c1 = '1', c2 = '0';
else flag = true, c1 = '0', c2 = '1';
}else{
if(i == 0 || !flag) c1 = c2 = '1';
else c1 = '2', c2 = '0';
}
a[i] = c1, b[i] = c2;
}
a[n] = '\0', b[n] = '\0';
puts(a), puts(b);
}
return 0;
}