C. Ternary XOR

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=abc=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, 1022211021=2121010222⊙11021=21210.

Your task is to find such ternary numbers aa and bb both of length nn and both without leading zeros that ab=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 (1t1041≤t≤104) — the number of test cases. Then tt test cases follow. The first line of the test case contains one integer nn (1n51041≤n≤5⋅104) — the length of xx. The second line of the test case contains ternary number xxconsisting 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 51045⋅104 (n5104∑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 ab=xa⊙b=xand max(a,b)max(a,b) is the minimum possible. If there are several answers, you can print any.

循环分配原数组,只要保证第一次出现1的时候分配给第一个,然后后面的都设置为0就可以保证max(a,b)最小

#define _CRT_SECURE_NO_WARNINGS
 
 
 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
typedef long long ll;
#define INF 99999999 
#define MAXN 50000
 
int m, n, t, k;
 
 
int main() {
    cin >> t;
    while (t--) {
        char a[MAXN] = { 0 };
        char b[MAXN] = { 0 };
        scanf("%d", &n);
        getchar();
        int last = 1;
        for (int i = 0; i < n; i++) {
            a[i] = getchar();
            b[i] = a[i];
            if (a[i] == '2') {
                if (last == 1) {
                    a[i] = '1';
                }
                else {
                    a[i] = '0';
                }
            }
            else if(a[i]=='1'){
                if (last == 1) {
                    last = 0;
                }
                else {
                    a[i] = '0';
                }
            }
        }
 
        for (int i = 0; i < n; i++) {
            printf("%c", a[i]);
        }printf("\n");
 
        for (int i = 0; i < n; i++) {
            printf("%d", b[i] - a[i]);
        }printf("\n");
 
    }
 
 
 
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Vetsama/p/12578782.html
xor