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

【题意】:https://www.cnblogs.com/bluefly-hrbust/p/8971769.html

本题题意就是把1到n数表示为,两组数之和相等,即能不能1->n划分成两部分(男女分组是干扰的)

1 2 3 4 5 6 7 8

偶数-->1+8+2+7 = 3+6+4+5

1 2 3 4 5 6 7

奇数-->1+3+4+6 = 2+5+7

偶数: n只需要前后匹配即可

奇数: n/2之前的奇数位和n/2之后的偶数位相加,等于n/2之前的偶数位加n/2的奇数位

【分析】:ACZone+

【出处】:CodeForces - 899C Dividing the numbers

【代码】:

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
int t,n,len;
char a[100050];
int b[100050];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
getchar();
scanf("%s",&a);
len=strlen(a);
if ((n%4)<=2 && n%4!=0)
{
printf("-1\n");
}
else
{
if(n%2==0) //偶数
{
for(int i=0; i<n/4; i++)
{
if (a[i]=='1')printf("3");
else printf("1");
}
for (int i=n/4; i<n-n/4; i++)
{
if (a[i]=='1')printf("4");
else printf("2");
}
for (int i=n-n/4; i<n; i++)
{
if (a[i]=='1')printf("3");
else printf("1");
}
printf("\n");
}
else //奇数
{
for(int i=0; i<n/2; i++)
{
if (a[i]=='1')
{
if ((i+1)%2==1)printf("3");
else printf("4");
}
else
{
if ((i+1)%2==1)printf("1");
else printf("2");
}
}
for(int i=n/2; i<len; i++)
{
if (a[i]=='1')
{
if ((i+1)%2==1)printf("4");
else printf("3");
}
else
{
if ((i+1)%2==1)printf("2");
else printf("1");
}
}
printf("\n");
}
}
}
return 0;
}

CONTINUE...?【构造/分析】的更多相关文章

  1. 任意文件下载漏洞的接口URL构造分析与讨论

    文件下载接口的URL构造分析与讨论 某学院的文件下载接口 http://www.****.edu.cn/item/filedown.asp?id=76749&Ext=rar&fname ...

  2. popchain与对应poc的构造分析

    本文首发于:https://mp.weixin.qq.com/s?__biz=MjM5MTYxNjQxOA==&mid=2652850238&idx=1&sn=6f22d8ab ...

  3. 《编译原理》构造 LL(1) 分析表的步骤 - 例题解析

    <编译原理>构造 LL(1) 分析表的步骤 - 例题解析 易错点及扩展: 1.求每个产生式的 SELECT 集 2.注意区分是对谁 FIRST 集 FOLLOW 集 3.开始符号的 FOL ...

  4. 编译原理根据项目集规范族构造LR(0)分析表

    转载于https://blog.csdn.net/Johan_Joe_King/article/details/79058597?utm_medium=distribute.pc_relevant.n ...

  5. 基于虎书实现LALR(1)分析并生成GLSL编译器前端代码(C#)

    基于虎书实现LALR(1)分析并生成GLSL编译器前端代码(C#) 为了完美解析GLSL源码,获取其中的信息(都有哪些in/out/uniform等),我决定做个GLSL编译器的前端(以后简称编译器或 ...

  6. 【编译原理】语法分析LL(1)分析法的FIRST和FOLLOW集

    近来复习编译原理,语法分析中的自上而下LL(1)分析法,需要构造求出一个文法的FIRST和FOLLOW集,然后构造分析表,利用分析表+一个栈来做自上而下的语法分析(递归下降/预测分析),可是这个FIR ...

  7. 【集合框架】JDK1.8源码分析之ArrayList详解(一)

    [集合框架]JDK1.8源码分析之ArrayList详解(一) 一. 从ArrayList字表面推测 ArrayList类的命名是由Array和List单词组合而成,Array的中文意思是数组,Lis ...

  8. 面向对象软件构造 (Bertrand Meyer 著)

    Part A: The Issues 议题 第一章 软件品质 第二章 面向对象的标准 Part B: The Road To Object Orientation 通向面向对象之路 第三章 模块性 第 ...

  9. 编译原理--02 自顶向下、自底向上的LR分析复习(清华大学出版社第3版)

    前言 目录 01 文法和语言.词法分析复习 02 自顶向下.自底向上的LR分析复习 03 语法制导翻译和中间代码生成复习 04 符号表.运行时存储组织和代码优化复习 第4章 自顶向下的语法分析方法 确 ...

随机推荐

  1. 网易考拉Android客户端网络模块设计

    本文来自网易云社区 作者:王鲁才 客户端开发中不可避免的需要接触到访问网络的需求,如何把访问网络模块设计的更具有扩展性是每一个移动开发者不得不面对的事情.现在有很多主流的网络请求处理框架,如Squar ...

  2. 什么时候会报unrecognized selector的异常?

    当调用该对象上某个方法,而该对象上没有实现这个方法的时候, 可以通过“消息转发”进行解决,如果还是不行就会报unrecognized selector异常 objc是动态语言,每个方法在运行时会被动态 ...

  3. python 之发送邮件服务[原著] 海瑞博客

    Python 发送邮件 使用默认的django的发送邮件,只适用于单邮箱. 作者:海瑞博客 http://www.hairuinet.com/ setting中配置 # send e-mail EMA ...

  4. lshw

    https://linux.die.net/man/1/lshw lshw(Hardware Lister)是另外一个可以查看硬件信息的工具,不仅如此,它还可以用来做一些硬件的benchmark. 这 ...

  5. php数组循环的三种方式

    PHP 的遍历数组的三种方式:for循环.foreach循环.while.list().each()组合循环 PHP当中数组分为:索引数组[转换成json是数组]和关联数组[转换成json是对象] f ...

  6. struts框架搭建

    struts是开源框架.使用Struts的目的是为了帮助我们减少在运用MVC设计模型来开发Web应用的时间.如果我们想混合使用Servlets和JSP的优点来建立可扩展的应用,struts是一个不错的 ...

  7. kafka-0.9消费者新API

    ## kafka-0.9消费者新API 注:以下仅限kafka版本0.9以上Consumer新版api Consumer自动提交示例: Properties props = new Propertie ...

  8. Sth about 函数式编程(Functional Programming)

    今天开会提到了函数式编程,针对不同类型的百年城方式,查阅了一部分资料,展示如下: 编程语言一直到近代,从汇编到C到Java,都是站在计算机的角度,考虑CPU的运行模式和运行效率,以求通过设计一个高效的 ...

  9. JS DOM对象与jQuery对象的转换

    JS转jQuery // 直接用$()来包裹 如同$(this) $(document) var jsObj = document.getElementById('test'); var jquery ...

  10. linux下对/sys/class/gpio中的gpio的控制 (转)

        在嵌入式设备中对GPIO的操作是最基本的操作.一般的做法是写一个单独驱动程序,网上大多数的例子都是这样的.其实linux下面有一个通用的GPIO操作接口,那就是我要介绍的 “/sys/clas ...