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. 《Cracking the Coding Interview》——第17章:普通题——题目1

    2014-04-28 21:45 题目:就地交换两个数,不使用额外的变量. 解法:没说是整数,我姑且先当整数处理吧.就地交换可以用加法.乘法.异或完成,其中乘法和加法都存在溢出问题.三种方法都不能处理 ...

  2. 《Cracking the Coding Interview》——第8章:面向对象设计——题目5

    2014-04-23 18:42 题目:设计一个在线阅读系统的数据结构. 解法:这题目太大了,我的个亲娘.显然你不可能一次加载一整本书,做到单页纸加载的粒度是很必要的.为了读书的连贯效果,预取个几页也 ...

  3. windows上php环境下memcache和mongodb的安装

    mangodb安装 1. 下载mongodb的安装文件,我安装的windows 64位的,下载地址如下: https://fastdl.mongodb.org/win32/mongodb-win32- ...

  4. 孤荷凌寒自学python第五十七天初次尝试使用python来连接远端MongoDb数据库

    孤荷凌寒自学python第五十七天初次尝试使用python来连接远端MongoDb数据库 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第三天.感觉这个东西学习起来还是那么困 ...

  5. React02

    目录 React 进阶提升 条件渲染 受控组件* 状态提升* 组件数据流 TODO-LIST 设置服务器端口 列表渲染 条目PropTypes检查类型 export & import Refs ...

  6. js 图片自动循环切换setInterval();

    stlye样式定义 <style type="text/css">             body{background-image: url(img/001.jpg ...

  7. hadoop2.5.2学习及实践笔记(三)—— HDFS概念及体系结构

    注:文中涉及的文件路径或配置文件中属性名称是针对hadoop2.X系列,相对于之前版本,可能有改动. 附: HDFS用户指南官方介绍: http://hadoop.apache.org/docs/r2 ...

  8. 洛谷 P2634 [国家集训队]聪聪可可 解题报告

    P2634 [国家集训队]聪聪可可 题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)--遇到这种问题,一 ...

  9. myeclipse maven web项目配置

    启用maven:window-->preference-->MyEclipse-->Maven4MyEclipse, 勾选复选框(Enable Mave4MyEclipse feat ...

  10. 理解javascript的闭包,原型,和匿名函数及IIFE

    理解javascript的闭包,原型,和匿名函数(自己总结) 一 .>关于闭包 理解闭包 需要的知识1.变量的作用域 例1: var n =99; //建立函数外的全局变量 function r ...