AtCoder Regular Contest 069 D
D - Menagerie
Time limit : 2sec / Memory limit : 256MB
Score : 500 points
Problem Statement
Snuke, who loves animals, built a zoo.
There are N animals in this zoo. They are conveniently numbered 1 through N, and arranged in a circle. The animal numbered i(2≤i≤N−1) is adjacent to the animals numbered i−1 and i+1. Also, the animal numbered 1 is adjacent to the animals numbered 2 and N, and the animal numbered N is adjacent to the animals numbered N−1 and 1.
There are two kinds of animals in this zoo: honest sheep that only speak the truth, and lying wolves that only tell lies.
Snuke cannot tell the difference between these two species, and asked each animal the following question: "Are your neighbors of the same species?" The animal numbered i answered si. Here, if si is o
, the animal said that the two neighboring animals are of the same species, and if si is x
, the animal said that the two neighboring animals are of different species.
More formally, a sheep answered o
if the two neighboring animals are both sheep or both wolves, and answered x
otherwise. Similarly, a wolf answered x
if the two neighboring animals are both sheep or both wolves, and answered o
otherwise.
Snuke is wondering whether there is a valid assignment of species to the animals that is consistent with these responses. If there is such an assignment, show one such assignment. Otherwise, print -1
.
Constraints
- 3≤N≤105
- s is a string of length N consisting of
o
andx
.
Input
The input is given from Standard Input in the following format:
N
s
Output
If there does not exist an valid assignment that is consistent with s, print -1
. Otherwise, print an string t in the following format. The output is considered correct if the assignment described by t is consistent with s.
- t is a string of length N consisting of
S
andW
. - If ti is
S
, it indicates that the animal numbered i is a sheep. If ti isW
, it indicates that the animal numbered i is a wolf.
Sample Input 1
6
ooxoox
Sample Output 1
SSSWWS
For example, if the animals numbered 1, 2, 3, 4, 5 and 6 are respectively a sheep, sheep, sheep, wolf, wolf, and sheep, it is consistent with their responses. Besides, there is another valid assignment of species: a wolf, sheep, wolf, sheep, wolf and wolf.
Let us remind you: if the neiboring animals are of the same species, a sheep answers o
and a wolf answers x
. If the neiboring animals are of different species, a sheep answers x
and a wolf answers o
.

Sample Input 2
3
oox
Sample Output 2
-1
Print -1
if there is no valid assignment of species.
Sample Input 3
10
oxooxoxoox
#include <bits/stdc++.h>
using namespace std;
const int N=;
int gh(char *str,char *ch,int n) {
for(int i=; i<n; i++) {
if(i<n-) {
if(str[i]=='o') {
if(ch[i]=='S') {
ch[i+]=ch[i-];
} else {
if(ch[i-]=='S') ch[i+]='W';
else ch[i+]='S';
}
} else {
if(ch[i]=='S') {
if(ch[i-]=='S') ch[i+]='W';
else ch[i+]='S';
} else {
ch[i+]=ch[i-];
}
}
}
else if(i==n-) {
if(str[i]=='o') {
if(ch[i]=='S') {
if(ch[i+]!=ch[i-]) {
return ;
}
}
else {
if(ch[i+]==ch[i-]){
return ;
}
}
}
else{
if(ch[i]=='S'){
if(ch[i+]==ch[i-]){
return ;
}
}
else{
if(ch[i+]!=ch[i-]) {
return ;
}
}
}
}
else if(i==n-){
if(str[i]=='x'){
if(ch[i]=='S') {
if(ch[n-]==ch[]) return ;
}
else{
if(ch[n-]!=ch[]) return ;
}
}
else{
if(ch[i]=='S'){
if(ch[n-]!=ch[]) return ;
}
else{
if(ch[n-]==ch[]) return ;
}
}
}
}
return ;
}
int main() {
char str[N];
char ch[N];
int n;
int flag=;
scanf("%d",&n);
scanf("%s",str);
ch[]='S';
if(str[]=='o') {
memset(ch,,sizeof(ch));
ch[]='S';
ch[]='S';
ch[n-]='S';
flag=gh(str,ch,n);
if(flag==) {
for(int i=; i<n; i++) printf("%c",ch[i]);
puts("");
return ;
} else {
memset(ch,,sizeof(ch));
ch[]='S';
ch[]='W';
ch[n-]='W';
flag=gh(str,ch,n);
if(flag==){
for(int i=;i<n;i++) printf("%c",ch[i]);
puts("");
return ;
}
}
}
else{
memset(ch,,sizeof(ch));
ch[]='S';
ch[]='S';
ch[n-]='W';
flag=gh(str,ch,n);
if(flag==){
for(int i=;i<n;i++) printf("%c",ch[i]);
puts("");
return ;
}
else{
memset(ch,,sizeof(ch));
ch[]='S';
ch[]='W';
ch[n-]='S';
flag=gh(str,ch,n);
if(flag==){
for(int i=;i<n;i++) printf("%c",ch[i]);
puts("");
return ;
}
}
}
ch[]='W';
if(str[]=='o'){
memset(ch,,sizeof(ch));
ch[]='W';
ch[]='S';
ch[n-]='W';
flag=gh(str,ch,n);
if(flag==){
for(int i=;i<n;i++) printf("%c",ch[i]);
puts("");
return ;
}
else{
ch[]='W';
ch[]='W';
ch[n-]='S';
flag=gh(str,ch,n);
if(flag==){
for(int i=;i<n;i++) printf("%c",ch[i]);
puts("");
return ;
}
}
}
else{
memset(ch,,sizeof(ch));
ch[]='W';
ch[]='S';
ch[n-]='S';
flag=gh(str,ch,n);
if(flag==){
for(int i=;i<n;i++) printf("%c",ch[i]);
puts("");
}
else{
ch[]='W';
ch[]='W';
ch[n-]='W';
flag=gh(str,ch,n);
if(flag==){
for(int i=;i<n;i++) printf("%c",ch[i]);
puts("");
return ;
}
}
}
puts("-1");
return ;
}
AtCoder Regular Contest 069 D的更多相关文章
- AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图
AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图 链接 AtCoder 大意 在数轴上放上n个点,点i可能的位置有\(x_i\)或者\(y_i\ ...
- AtCoder Regular Contest 069 D - Menagerie 枚举起点 模拟递推
arc069.contest.atcoder.jp/tasks/arc069_b 题意:一堆不明身份的动物排成一圈,身份可能是羊或狼,羊一定说实话,狼一定说假话.大家各自报自己的两边是同类还是不同类, ...
- AtCoder Regular Contest 069 F - Flags
题意: 有n个点需要摆在一个数轴上,每个点需要摆在ai这个位置或者bi上,问怎么摆能使数轴上相邻两个点之间的距离的最小值最大. 二分答案后显然是个2-sat判定问题,因为边很多而连边的又是一个区间,所 ...
- AtCoder Regular Contest 069
1. C - Scc Puzzle 计算scc的个数,先判断s个数需要多少个cc,多的cc,每四个可以组成一个scc.注意数据范围,使用long long. #include<bits/stdc ...
- AtCoder Regular Contest 061
AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...
- AtCoder Regular Contest 094 (ARC094) CDE题解
原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...
- AtCoder Regular Contest 092
AtCoder Regular Contest 092 C - 2D Plane 2N Points 题意: 二维平面上给了\(2N\)个点,其中\(N\)个是\(A\)类点,\(N\)个是\(B\) ...
- AtCoder Regular Contest 093
AtCoder Regular Contest 093 C - Traveling Plan 题意: 给定n个点,求出删去i号点时,按顺序从起点到一号点走到n号点最后回到起点所走的路程是多少. \(n ...
- AtCoder Regular Contest 094
AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...
随机推荐
- 9.python面向对象编程
面向对象的几个核心特性如下 Class 类一个类即是对一类拥有相同属性的对象的抽象.蓝图.原型.在类中定义了这些对象的都具备的属性(variables(data)).共同的方法 Object 对象 一 ...
- Python3 学习Python流程--试水中
二.基础语法之后可以搭载服务器练习: 教程 一.1.Python 搭建环境. 初学基本语法 :Python基本语法 2.推荐 IDE : PyCharm CE 下载 菜鸟教程都是基础语法,可以对py ...
- iOS 写给iOS开发者的React Native学习路线(转)
我是一名iOS开发者,断断续续一年前开始接触React Native,最近由于工作需要,专职学习React Native也有一个多月了.网络上知识资源非常的多,但能让人豁然开朗.迅速学习的还是少数,我 ...
- Intellij IDEA 像eclipse那样给maven添加依赖
打开pom.xml,在它里面使用快捷键:ALT+Insert ---->点击dependency 再输入想要添加的依赖关键字,比如:输个spring 出现下图: 根据需求选择版本,完成以后 ...
- DeepLearning.ai学习笔记(三)结构化机器学习项目--week2机器学习策略(2)
一.进行误差分析 很多时候我们发现训练出来的模型有误差后,就会一股脑的想着法子去减少误差.想法固然好,但是有点headlong~ 这节视频中吴大大介绍了一个比较科学的方法,具体的看下面的例子 还是以猫 ...
- 童话故事 --- 什么是SQL Server Browser
高飞狗这几天特别郁闷,不知该如何通过TCP/IP协议连接SQL Server数据库.好在功夫不负有心人,经过几天的刻苦研究,终于得到了答案. 高飞狗呼叫UDP1434端口,"叮铃铃,叮铃铃- ...
- 自动化测试辅助工具(Selenium IDE等)
本随表目录 Selenium IDE安装和使用 FireBug安装和使用 FirePath安装和使用 Selenium IDE安装 方式一:打开Firefox-->添加组件-->搜索出 ...
- vue实现登录后跳转到之前的页面
在开发中我们经常遇到这样的需求,需要用户直接点击一个链接进入到一个页面,用户点击后链接后会触发401拦截返回登录界面,登录后又跳转到链接的页面而不是首页,这种问题该如何去做呢? 先说一下我们需要用到的 ...
- 【Java框架型项目从入门到装逼】第七节 - 学生管理系统项目搭建
本次的教程是打算用Spring,SpringMVC以及传统的jdbc技术来制作一个简单的增删改查项目,对用户信息进行增删改查,就这么简单. 1.新建项目 首先,打开eclipse,新建一个web项目. ...
- 机器学习小记——KNN(K近邻) ^_^ (一)
为了让绝大多数人都可以看懂,所以我就用简单的话语来讲解机器学习每一个算法 第一次写ML的博文,所以可能会有些地方出错,欢迎各位大佬提出意见或错误 祝大家开心进步每一天- 博文代码全部为python 简 ...