浙江省赛zoj (4003)

CONTINUE...?

Time Limit: 1 Second       Memory Limit: 65536 KB       Special Judge

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

DreamGrid would like to divide the classmates into four groups  and  such that:

  • Each classmate belongs to exactly one group.

  • Both  and  consist only of girls. Both  and  consist only of boys.

  • The total number of gems in  and  is equal to the total number of gems in  and .

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  () -- the number of classmates.

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

It is guaranteed that the sum of all  does not exceed .

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

#include<iostream>
#include<cstdio>
#include<queue>
#include<map>
#include<cstring>
#include<algorithm>
#include<string>
#define LL long long
using namespace std;
char str[200015];
int main()
{
    int tt;
    cin>>tt;
    while(tt--)
    {
        int n;
        scanf("%d",&n);
        scanf("%s",str+1);
        if(n%4==1||n%4==2)//总和为奇数一定不行
        {
            cout<<-1<<endl;
        }
        if(n==3)//n=3特判
        {
            for(int i=1;i<=n-1;i++)
           {
               if(str[i]=='1')
               {
                   printf("3");
               }
               if(str[i]=='0')
               {
                   printf("1");
               }
           }
           if(str[n]=='1')
           {
               printf("4");
           }
           if(str[n]=='0')
           {
               printf("2");
           }
           cout<<endl;
        }
        else if(n%4==3||n%4==0)//两边为一组,中间为一组
        {
            int t1=n/4+1;//左端点n/4+1
            int t2=n-t1;//右端点n-t1
            if(n%4==0)t2++;//如果是n%4=0的情况右端点右移一位
            for(int i=1;i<t1;i++)
           {
               if(str[i]=='1')
               {
                   printf("3");
               }
               if(str[i]=='0')
               {
                   printf("1");
               }
           }
           for(int i=t1;i<=t2;i++)
           {
               if(str[i]=='1')
               {
                   printf("4");
               }
               if(str[i]=='0')
               {
                   printf("2");
               }
           }
           for(int i=t2+1;i<=n;i++)
           {
               if(str[i]=='1')
               {
                   printf("3");
               }
               if(str[i]=='0')
               {
                   printf("1");
               }
           }
           cout<<endl;
        }
    }
}
/*
                1 2 3 4 5 6 7   2-5
                1 2 3 4 5 6 7 8 9 10 11  3- 8
                1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 4-11
                1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 5-14
                1 2 3 4 5 6 7 8 3-6
                */


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值