Description

There are nnn blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.

You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).

You want to find a sequence of operations, such that they make all the blocks having the same color. You don’t have to minimize the number of operations, but it should not exceed 3⋅n3⋅n3⋅n. If it is impossible to find such a sequence of operations, you need to report it.

Input

The first line contains one integer n(2≤n≤200)n(2≤n≤200)n(2≤n≤200) — the number of blocks.

The second line contains one string s consisting of n characters, each character is either “W” or “B”. If the i-th character is “W”, then the i-th block is white. If the i-th character is “B”, then the i-th block is black.

Output

If it is impossible to make all the blocks having the same color, print −1−1−1.

Otherwise, print an integer k(0≤k≤3⋅n)k(0≤k≤3⋅n)k(0≤k≤3⋅n) — the number of operations. Then print kkk integers p1,p2,…,pk(1≤pj≤n−1)p_1,p_2,…,p_k (1≤p_j≤n−1)p1​,p2​,…,pk​(1≤pj​≤n−1), where pjp_jpj​ is the position of the left block in the pair of blocks that should be affected by the jjj-th operation.

If there are multiple answers, print any of them.

题意

给定一串黑白文本,每次可以将其中相邻2个颜色翻转,求一个可行的操作序列使得操作后颜色相同。

如果不能找到输出-1

思路

一开始看到n<=200n<=200n<=200直接打了一发爆搜+记忆化,然后MLE炸到飞起……

正解是假定操作后全白,从头扫到尾一次,假定全黑,从头扫到尾一次,看看能否成功。

比如:我们要全白,而此时颜色是 黑白黑黑黑

可以将黑视作高台阶,白视作低台阶,然后一路推过去,最后能推平就可以了。



推平位置1后,往后找到位置2,推平位置2后,2 3都平了,再往后遍历找到位置4,推平位置4后,全部推平,合法。

因此操作序列就为:1 2 4

显然这样操作只会有2种结果:全平或者最后一个不平。

全黑全白两个都扫一遍就好了,复杂度O(n)O(n)O(n)

Code

#include <cstdio>
#include <cstring>
using namespace std;
int n,len;
char all[201];
char temp[201];
int path[201];
int tot;
bool checkblack()
{
memcpy(temp,all,sizeof(all));
tot = 0;
for(int i = 1;i<len;++i)
{
if(temp[i] == 'W')
{
temp[i] = 'B';
temp[i+1] = (temp[i+1] == 'W' ? 'B' : 'W');
path[++tot] = i;
}
}
return temp[len] == 'B';
}
bool checkwhite()
{
memcpy(temp,all,sizeof(all));
tot = 0;
for(int i = 1;i<len;++i)
{
if(temp[i] == 'B')
{
temp[i] = 'W';
temp[i+1] = (temp[i+1] == 'W' ? 'B' : 'W');
path[++tot] = i;
}
}
return temp[len] == 'W';
}
int main()
{
scanf("%d",&n);
scanf("%s",all+1);
len = strlen(all+1);
if(checkblack() || checkwhite())
{
printf("%d\n",tot);
for(int i =1 ;i<=tot;++i)
printf("%d ",path[i]);
}
else
printf("-1");
return 0;
}

[Codeforces #608 div2]1272B Blocks的更多相关文章

  1. [Codeforces #608 div2]1271D Portals

    Description You play a strategic video game (yeah, we ran out of good problem legends). In this game ...

  2. [Codeforces #608 div2]1271C Shawarma Tent

    Description The map of the capital of Berland can be viewed on the infinite coordinate plane. Each p ...

  3. [Codeforces #608 div2]1271A Suits

    Description A new delivery of clothing has arrived today to the clothing store. This delivery consis ...

  4. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  5. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  6. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  7. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  8. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  9. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

随机推荐

  1. 设计模式01 创建型模式 - 建造者模式(Build Pattern)

    参考 1. Builder Design Pattern | Youtube 2. 建造者模式(Builder和Director)| 博客园 3. 深入理解Builder模式 | 简书 建造者模式(B ...

  2. 融资拿钱----HHR计划---第四课

    第一节:开始学习 1,预热思考题: (1)投资人愿意投你?你想过你的投资逻辑是什么吗?(赚钱=行业天花板*成功的概率=细分行业天花板*集中度*whynow*whyme) (2)融资就是为了拿钱吗?你了 ...

  3. SQL模糊匹配之正则表达式

    −      方括号[ ]:指定一个字符.字符串.匹配他们中的任意一个. −      示例1:查询用户名以J或者以M开头的用户信息 −      SELECT user_name FROM ecs_ ...

  4. Python学习第十八课——继承,接口继承等

    1.继承:字面意思 # 继承 : 字面意思 class father: pass class grandfather: pass class children(father): # 单继承 pass ...

  5. gcd && exgcd算法

    目录 欧几里德算法与扩展欧几里德算法 1.欧几里德算法 2.扩展欧几里德算法 欧几里德算法与扩展欧几里德算法 1.欧几里德算法 #include<bits/stdc++.h> using ...

  6. 快速幂 & 矩阵快速幂

    目录 快速幂 实数快速幂 矩阵快速幂 快速幂 实数快速幂 普通求幂的方法为 O(n) .在一些要求比较严格的题目上很有可能会超时.所以下面来介绍一下快速幂. 快速幂的思想其实是将数分解,即a^b可以分 ...

  7. Fiddler抓包(基本使用方法、web+app端抓包、篡改数据、模拟低速)

    1.HTTP代理原理图 http服务器代理:既是web服务器,又是web客户端 接口vs端口: 接口:包含地址和端口 端口:类似于USB接口 地址:127.0.0.1,端口默认:8888        ...

  8. 安装mysql过程中的异常解决

      [root@cdh1 ruanjian]# rpm -ivh mysql-community-common-5.7.10-1.el6.x86_64.rpm  warning: mysql-comm ...

  9. idea右键新建选项没有类和包的创建方式

    Intelidea创建好项目之后,右键新建Java class的时候发现没有改选项,只有以下几个选项 把sec目录设为源码目录,首先打开Project Structure

  10. 常用的sql指令

    一,关于数据库中表的一些介绍 1,表的每一行表示关系的一个元组,又称记录: 2,表的每一列表示关系的一个字段,又称属性(例如:id.name.password........): 3,  表结构:表的 ...