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=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.
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).
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.
题目地址:https://codeforces.com/contest/1328/problem/C
思路:if else 判断,定义两个答案串ans1,ans2,让ans1大于ans2,当ans1不比ans2大时,让ans1得到较大的数,当ans1大于ans2时,ans1得到较小的数.(结合代码食用)
AC代码:
#include <stdio.h> #include <iostream> #include <vector> #include <algorithm> #include <queue> #include <map> #include<math.h> #include<stack> #include <queue> using namespace std; #define maxn 10010 #define Minf -1e9 typedef long long ll; #define INF 1e9 int main() { int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; string s1, s2; int flag = 0; int len = s.size(); if (s[0] == '1') { s1+= '3'; s2 += '1'; flag = 1; } if (s[0] == '2') { s1 += '1'; s2+= '1'; } for (int i = 1; i < len; i++) { if (!flag) { if (s[i] == '0') { s1 += '0'; s2 += '0'; } else if (s[i] == '1') { s1 += '1'; s2 += '0'; flag = 1; } else { s1 += '1'; s2 += '1'; } } else { if (s[i] == '0') { s1 += '0'; s2 += '0'; } else if (s[i] == '1') { s1 += '0'; s2 += '1'; } else { s1 += '0'; s2 += '2'; } } } cout << s1 << endl << s2 << endl; } }

浙公网安备 33010602011771号