题目链接:http://codeforces.com/contest/758/problem/B

题意:给定n个点灯的情况,灯只有四种颜色RBGY,然后如果某个灯坏了则用'!'表示,现在要求将坏的灯(即'!'的位置)用其他新的灯来替换(新灯只有前面描述的四种颜色),并且要满足最后的灯序列每连续四个位置的灯的颜色都不一样(即分别是给定的四种颜色,顺序任意)。问最后要用每种颜色的灯的数目。题目保证输入一定合法,即一定存在一个符合条件的序列

思路:因为每连续四个位置要满足颜色不一样的要求,所以我们可以单独枚举前四个位置坏的灯的颜色,然后后面的位置就可以递推来求得。用二进制的1111标准四种颜色灯出现的次数,1表示出现了,0表示没有出现。然后如果四个位置的值不为15(二进制表示1111)表示这个序列不符合题目要求,继续枚举下一种情况。

import java.io.PrintWriter;
import java.util.*; public class Main {
public static boolean flag;
public static String color = "RBYG";
public static StringBuffer s;
public static int ans[] = new int[color.length()]; public static void dfs(int idx,StringBuffer str) {
if (flag == true) {
return;
}
if (idx >= 4) {
flag = check(new StringBuffer(str));
return;
}
if (str.charAt(idx) == '!') {
for (int i = 0; i < 4 && flag == false; i++) {
str.setCharAt(idx, color.charAt(i));
dfs(idx + 1,new StringBuffer(str));
}
} else {
dfs(idx + 1,new StringBuffer(str));
}
} public static boolean check(StringBuffer str) {
int val = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '!') {
for (int j = 0; j < 4; j++) {
if ((val & (1 << j)) == 0) {
str.setCharAt(i, color.charAt(j));
val |= (1 << j);
}
}
} else {
val |= (1 << color.indexOf(str.charAt(i)));
}
if (i >= 3 && val != 15) {
return false;
}
if (i >= 3) {
val ^= (1 << color.indexOf(str.charAt(i - 3)));
}
}
s=str;
return true;
} public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
String str = cin.next();
s = new StringBuffer(str);
flag = false;
dfs(0,new StringBuffer(str));
Arrays.fill(ans, 0);
//out.println(s);
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '!') {
ans[color.indexOf(s.charAt(i))]++;
}
}
for (int i = 0; i < 4; i++) {
out.printf("%d", ans[i]);
out.printf("%c", i == 3 ? '\n' : ' ');
}
cin.close();
out.flush();
}
}

Codeforces Round #392 (Div. 2) - B的更多相关文章

  1. Codeforces Round #392 (Div. 2) F. Geometrical Progression

    原题地址:http://codeforces.com/contest/758/problem/F F. Geometrical Progression time limit per test 4 se ...

  2. Virtual Codeforces Round #392 (Div. 2)

    下午闲来无事开了一场Virtual participation 2h就过了3道水题...又跪了..这只是Div. 2啊!!! 感觉这次直接就是跪在了读题上,T1,T2读题太慢,T3还把题读错了 要是让 ...

  3. Codeforces Round #392 (Div. 2) - C

    题目链接:http://codeforces.com/contest/758/problem/C 题意:给定N*M矩阵的教室,每个位置都有一个学生,Sergei坐在[X,Y],然后老师会问K个问题,对 ...

  4. Codeforces Round #392 (Div. 2) - A

    题目链接:http://codeforces.com/contest/758/problem/A 题意:给定N个城市的福利,国王现在想让每个城市的福利都一致.问最少需要花多少钱使得N个城市的福利值都一 ...

  5. Codeforces Round #392 (Div. 2)-758D. Ability To Convert(贪心,细节题)

    D. Ability To Convert time limit per test 1 second Cmemory limit per test 256 megabytes input standa ...

  6. Codeforces Round #392 (Div. 2)

    D题,给出n,k,k是n进制数,但是大于十进制时,它的表示方法仍为十进制那种,比如16进制下的15,我们可以看成就是15,或者1|5,也就是1×16+5 = 21,让你求出能表达的最小十进制数 从后面 ...

  7. Codeforces Round #392 (Div. 2)-D. Ability To Convert

    D - Ability To Convert 题目大意:给你一个数字 n 接下来再输入一个数字 w(<10^60),表示w这个数字是 n 进制的, 并且超过十进制也用数字表示,这样就有多种组合了 ...

  8. Codeforces Round #392 (Div. 2) A B C 水 模拟 暴力

    A. Holiday Of Equality time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. Codeforces Round #392 (div.2) E:Broken Tree

    orz一开始想不画图做这个题(然后脑袋就炸了,思维能力有待提高) 我的做法是动态规划+贪心+构造 首先把题目给的树变成一个可行的情况,同时weight最小 这个可以通过动态规划解决 dp[x]表示以x ...

随机推荐

  1. Vue项目安装CubeUI

    CubeUi GitHub地址:https://github.com/didi/cube-ui install vue add cube-ui 项目中 会多出cube ui 的配置文件来

  2. JavaScript用在哪里

    <script> 标签 在HTML,JavaScript代码必须插入<script> 和 </script>之间的. document.getElementById ...

  3. Flask学习笔记02之配置文件

    1. Flask默认的配置 Flask实例中包含了它的配置信息 #实例化一个Flask对象 app = Flask(__name__) # 打印默认配置信息 print(app.config) 打印结 ...

  4. 用树状数组写的最长上升子序列(友好城市),nlogn。

    #include<iostream> #include<algorithm> #define maxn 100000 #define lb(x) x&-x using ...

  5. phpexcel如何读和写大于26列的excel

    主要运用到PHPExcel_Cell类的两个方法 1读取excel大于26列时. PHPExcel_Cell::columnIndexFromString($highestColumm)://由列名转 ...

  6. hud 4347 The Closest M Points(KD-Tree)

    传送门 解题思路 \(KD-Tree\)模板题,\(KD-Tree\)解决的是多维问题,它是一个可以储存\(K\)维数据的二叉树,每一层都被一维所分割.它的插入删除复杂度为\(log^2 n\),它查 ...

  7. 北风设计模式课程---备忘录(Memento)模式

    北风设计模式课程---备忘录(Memento)模式 一.总结 一句话总结: 备忘录模式也是一种比较常用的模式用来保存对象的部分用于恢复的信息,和原型模式有着本质的区别,广泛运用在快照功能之中.同样的使 ...

  8. 126B Password[扩展kmp学习]

    题目大意 给你一个字符串,求它的一个子串使得这个子串即使前缀又是后缀又出现在不是前缀且不是后缀的地方 分析 扩展kmp就是定义z[i]表示i~n的子串与整个串的最长公共前缀的长度是z[i] 所以这个题 ...

  9. npm install 安装不成功,提示python2.7

    npm install 安装不成功的原因 是因为缺少python的环境 解决方法: 1.去官网下载https://www.python.org/download/releases/2.7/ 2.安装成 ...

  10. 利用Fiddler对手机应用抓包

    1.启动Fiddler,打开菜单栏中的 Tools > Fiddler Options,打开“Fiddler Options”对话框. 2.在Fiddler Options”对话框切换到“Con ...