zoukankan      html  css  js  c++  java
  • CodeForces 1328C

    A number is ternary if it contains only digits 0, 1 and 2. For example, the following numbers are ternary: 1022, 11, 21, 2002.

    You are given a long ternary number x. The first (leftmost) digit of x is guaranteed to be 2, the other digits of x can be 0, 1 or 2.

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

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

    You have to answer t independent test cases.

    Input

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

    Output

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

    Example Input

    4
    5
    22222
    5
    21211
    1
    2
    9
    220222021

    Output

    11111
    11111
    11000
    10211
    1
    1
    110111011
    110111010

    如果数字仅包含数字0、1和2,则该数字是三进制的。例如,以下数字是三进制的:1022、11、21、2002。

    给您一个长的三进制数x。 x的第一个(最左)数字保证为2,x的其他数字可以为0、1或2。

    让我们将两个三进制数a和b(均为长度n)的三进制XOR运算define定义为长度为n的数字c =a⊙b,其中ci =(ai + bi)%3(其中%为模运算)。换句话说,将相应的数字相加,然后求和除以3所得的余数。例如10222⊙11021= 21210。

    您的任务是找到长度为n且都不带前导零的三元数a和b,使得a⊙b= x和max(a,b)最小。

    您必须回答t个独立的测试用例。

    输入值
    输入的第一行包含一个整数t(1≤t≤104)-测试用例的数量。然后是t个测试用例。测试用例的第一行包含一个整数n(1≤n≤5⋅104)-x的长度。测试用例的第二行包含由n位数字0,1或2组成的三进制数x。保证x的第一位是2。保证在所有测试用例中n的总和不超过5⋅。 104(∑n≤5e45)。

    输出量
    对于每个测试用例,请打印答案-两个三元整数a和b,它们的长度均为n且都没有前导零,因此a⊙b= x和max(a,b)是最小可能的。如果有多个答案,则可以打印任何答案。


    输入
    4
    5
    22222
    5
    21211
    1个
    2
    9
    220222021
    输出
    11111
    11111
    11000
    10211
    1
    1
    110111011
    110111010

    题目大意:
    输入t表示有t组测试样例,对于每组测试样例,输入一个 n 表示该三进制串长,然后输入一个长度为n的三进制串,对于每组输入要输出a串和b串,要求是a ^ b = 输入的串,并且max(a,b) 要最小。
    解题思路:
    要使a ^ b得到x,只要分解x的每一位就可以,比如1可以分解成0 1,2可以分解成1 1 和2 0,0的话就是0 0了,但是要使max(a,b)最小,我们就要让a和b尽可能的接近,我们遍历整个串,因为要尽可能接近,当没遇到1之前,2全部分解成1 1 ,当遇到1时,必然要分解成1 和 0,这个时候a串和b串一定就有差距了。要让ab尽可能接近,之后的2分解成2 0,1分解成1 0,分解第一个遇到的1时,我们要分解成1 0,所以再以后遇到1或2时,让继承了0的串继承大的数,继承1的串继承小的数,就可以让ab尽可能接近了。举个栗子:
    2121这个串 第一个2时,ab都接受一个1,这时a 1 b 1,第二个遇到了1,这时让a 串接受0,b串接受1,就成了a 10 b 11,b比a大了,因为b的高位比a大,所以后面均不影响ab比较的结果,所以对于低位,我们让b尽可能小,a尽可能大就可以,分解的最后结果是 a:1021 b: 1100 这时,a ^ b = 2121,max(a,b)最小。 AC代码:

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    using namespace std;
    int main()
    {
            ios::sync_with_stdio(false);
    	int t;
    	cin>>t;
    	while (t--)
    	{
    		int n;
    		cin>>n;
    		string s,a,b;
    		cin>>s;
    		bool flag=false;//用于记录是否已经碰到了1
    		
    		for(int i=0;i<s.length();i++)
    		{
    			if(s[i]=='2')
    			{
    				if(flag)
    				{
    					a+='0';
    					b+='2';
    				}
    				else
    				{
    					a+='1';
    					b+='1';
    				}
    			}
    			if(s[i]=='1')
    			{
    				if(flag)
    				{
    					a+='0';
    					b+='1';
    				}
    				else
    				{
    					a+='1';
    					b+='0';
    					flag=true;//碰到第一个1时 发生改变
    				}	
    			}
    			if(s[i]=='0')
    			{
    				a+='0';
    				b+='0';
    			}
    		}
    		cout<<a<<endl<<b<<endl;
    	}
    	//system("pause");
    	return 0;
    }
    
  • 相关阅读:
    linux c/c++ 获取文件大小
    android 打开各种文件(setDataAndType)
    Android framework系统默认设置修改
    android的 root权限
    [Power]待机电流问题,如何查找wakelock
    Android.mk for your own module
    通过adb 发送广播
    ubuntu下minicom的安装及使用
    ubuntu 下使用 putty 调试
    Android平台Overlay机制
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294268.html
Copyright © 2011-2022 走看看