ZOJ--CONTINUE...?

CONTINUE...?

Time Limit: 1000 msMemory Limit: 65536 KB

DreamGrid has n classmates numbered from 1 to n. Some of them are boys and the others are girls. Each classmate has some gems, and more specifically, the i-th classmate has i gems.

DreamGrid would like to divide the classmates into four groups G​1​​, G​2​​, G​3​​ and G​4​​ such that:

  • Each classmate belongs to exactly one group.

  • Both G​1​​ and G​2​​ consist only of girls. Both G​3​​ and G​4​​ consist only of boys.

  • The total number of gems in G​1​​ and G​3​​ is equal to the total number of gems in G​2​​ and G​4​​.

Your task is to help DreamGrid group his classmates so that the above conditions are satisfied. Note that you are allowed to leave some groups empty.

Input

There are multiple test cases. The first line of input is an integer  indicating the number of test cases. For each test case:

The first line contains an integer n (1≤n≤10​5​​) -- the number of classmates.

The second line contains a string s (∣s∣=n) consisting of 0 and 1. Let s​i​​ be the i-th character in the string s. If s​i​​=1, the i-th classmate is a boy; If s​i​​=0, the i-th classmate is a girl.

It is guaranteed that the sum of all n does not exceed 10​6​​.

Output

For each test case, output a string consists only of {1, 2, 3, 4}. The -th character in the string denotes the group which the -th classmate belongs to. If there are multiple valid answers, you can print any of them; If there is no valid answer, output "-1" (without quotes) instead.

Sample Input

5
1
1
2
10
3
101
4
0000
7
1101001

Sample Output

-1
-1
314
1221
3413214

Author: LIN, Xi

 

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
char s[100010];

int main()
{
	
	ios::sync_with_stdio(false);
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		cin>>n>>s;
		if((n*(n+1)/2)%2!=0)
		{
			cout<<"-1"<<endl;
			continue;
		}
		else
		{
			if(n%2==0)
			{
				for(int i=0;i<n;i++)
				{
					if(i%4==0||i%4==3)
					{
						if(s[i]=='0')
						s[i]='1';
						else
						s[i]='3';
					}
					else
					{
						if(s[i]=='0')
						s[i]='2';
						else
						s[i]='4';
					}
				}
			 
			}
			else
			{
				if(s[0]=='0')s[0]='1';
				else s[0]='3';
				for(int i=1;i<n;i++)
				{
					if(i%4==1||i%4==0)
					{
						if(s[i]=='0')
						s[i]='1';
						else
						s[i]='3';
					}
					else
					{
						if(s[i]=='0')
						s[i]='2';
						else
						s[i]='4';
					}
				}
			}
			for(int i=0;i<n;i++)
			cout<<s[i];
			cout<<endl;
		}
	}
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值