C. Ternary XOR

Problem - C - Codeforces

A number is ternary if it contains only digits 00, 11 and 22. For example, the following numbers are ternary: 10221022, 1111, 2121, 20022002.

You are given a long ternary number xx. The first (leftmost) digit of xx is guaranteed to be 22, the other digits of xx can be 00, 11 or 22.

Let's define the ternary XOR operation ⊙⊙ of two ternary numbers aa and bb (both of length nn) as a number c=a⊙bc=a⊙b of length nn, where ci=(ai+bi)%3ci=(ai+bi)%3 (where %% is modulo operation). In other words, add the corresponding digits and take the remainders of the sums when divided by 33. For example, 10222⊙11021=2121010222⊙11021=21210.

Your task is to find such ternary numbers aa and bb both of length nn and both without leading zeros that a⊙b=xa⊙b=x and max(a,b)max(a,b) is the minimum possible.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. Then tt test cases follow. The first line of the test case contains one integer nn (1≤n≤5⋅1041≤n≤5⋅104) — the length of xx. The second line of the test case contains ternary number xx consisting of nn digits 0,10,1 or 22. It is guaranteed that the first digit of xx is 22. It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1045⋅104 (∑n≤5⋅104∑n≤5⋅104).

Output

For each test case, print the answer — two ternary integers aa and bb both of length nn and both without leading zeros such that a⊙b=xa⊙b=x and max(a,b)max(a,b) is the minimum possible. If there are several answers, you can print any.

Example

input

Copy

4
5
22222
5
21211
1
2
9
220222021

output

Copy

11111
11111
11000
10211
1
1
110111011
110111010
只要比较字符串大小即可
#include<iostream>
using namespace std;
string a,b;
int t,n;
string s;
int main()
{
    cin>>t;
    while(t--)
  {
    cin>>n;
    cin>>s;
    a.clear();
    b.clear();
    int flag=0;
    for(int i=0;s[i];i++)
    {
        if(s[i]=='2')
        {
           if(a>b)
           {
               a+='0';
              b+='2';
           }
           else if(a==b)
           {
               a+='1';
               b+='1';
           }
           else
           {
               a+='2';
               b+='0';
           }
        }
        else if(s[i]=='0')
        {
            a+='0';
            b+='0';
        }
        else//1
        {
            if(a>b)
            {
                a+='0';
                b+='1';
            }
            else
            {
                a+='1';
                b+='0';
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        cout<<a[i];
    }
    cout<<endl;
    for(int i=0;i<n;i++)
    {
        cout<<b[i];
    }
    cout<<endl;
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值