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≤iN−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 and x.

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 and W.
  • If ti is S, it indicates that the animal numbered i is a sheep. If ti is W, 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

Sample Output 3

SSWWSSSWWS

题目链接:http://arc069.contest.atcoder.jp/tasks/arc069_b

题意:n只动物从1到n围成一个圈,每只动物要么是羊要么是狼。每只动物会说出一个字母,说'o'表示它两边动物种类相同,说'x'表示不同。但羊是说真话,狼是说反话。求出这n只动物的种类。
分析:模拟一下就可以了,不过这个模拟比较大!
下面给出AC代码:
 #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的更多相关文章

  1. AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图

    AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图 链接 AtCoder 大意 在数轴上放上n个点,点i可能的位置有\(x_i\)或者\(y_i\ ...

  2. AtCoder Regular Contest 069 D - Menagerie 枚举起点 模拟递推

    arc069.contest.atcoder.jp/tasks/arc069_b 题意:一堆不明身份的动物排成一圈,身份可能是羊或狼,羊一定说实话,狼一定说假话.大家各自报自己的两边是同类还是不同类, ...

  3. AtCoder Regular Contest 069 F - Flags

    题意: 有n个点需要摆在一个数轴上,每个点需要摆在ai这个位置或者bi上,问怎么摆能使数轴上相邻两个点之间的距离的最小值最大. 二分答案后显然是个2-sat判定问题,因为边很多而连边的又是一个区间,所 ...

  4. AtCoder Regular Contest 069

    1. C - Scc Puzzle 计算scc的个数,先判断s个数需要多少个cc,多的cc,每四个可以组成一个scc.注意数据范围,使用long long. #include<bits/stdc ...

  5. AtCoder Regular Contest 061

    AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...

  6. AtCoder Regular Contest 094 (ARC094) CDE题解

    原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...

  7. AtCoder Regular Contest 092

    AtCoder Regular Contest 092 C - 2D Plane 2N Points 题意: 二维平面上给了\(2N\)个点,其中\(N\)个是\(A\)类点,\(N\)个是\(B\) ...

  8. AtCoder Regular Contest 093

    AtCoder Regular Contest 093 C - Traveling Plan 题意: 给定n个点,求出删去i号点时,按顺序从起点到一号点走到n号点最后回到起点所走的路程是多少. \(n ...

  9. AtCoder Regular Contest 094

    AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...

随机推荐

  1. 将java项目打包成docker镜像

    简介:将jar打包成镜像好说,毕竟jar包长的都是一样的,但是我们只是写了一个普通的java项目,我也不方便封装成jar包什么的,但是我们也想打包docker image怎么办呢,我们可以用编译后的j ...

  2. 小白的Python之路 day2 文件操作

    文件操作 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...

  3. 两个HC-05蓝牙模块互相绑定构成无线串口模块

    HC-05 嵌入式蓝牙串口通讯模块(以下简称模块)具有两种工作模式:命令响应工作模式和自动连接工作模式,在自动连接工作模式下模块又可分为主(Master).从(Slave)和回环(Loopback)三 ...

  4. 关于PLC

    学电气的一方面是单片机,一方面是PLC,,,,常常看到说选择比努力更重要,,单片机都很熟悉了,我就来介绍一下PLC..... 然后呢我先吹吹牛,,,目的是让大家相信我介绍的PLC绝对是亲身体验.... ...

  5. ES6 let和const命令(2)

    为什么要使用块级作用域 在ES5中只有全局作用域和函数作用域,没有块级作用域,因此带来了这些麻烦 内层变量可能会覆盖外层变量 var tmp = new Date(); console.log(tmp ...

  6. Saltstack的安装和配置

    1.安装salt 因为系统自带的yum源不支持saltstack安装包的支持,所以需要安装第三方yum源(epel) # yum -y install epel-release salt分为主服务器( ...

  7. for 在项目实战中用的比较多

    for循环编程语言中的语句之一,用于循环执行.for循环是开界的,它的一般形式为: for(; <条件表达式>; ) 语句: 初始化通常是一个赋值语句, 它用来给循环控制变量赋初值: 条件 ...

  8. Python函数篇(7)-正则表达式

    1.正则表达式   正则表达式为高级的文本模式匹配,抽取,与/或文本形式的搜索和替换功能提供了基础,简单的来说,正则表达式是由一些字符和特殊符号组成的字符串.Python通过标准库中的re模块来支持正 ...

  9. Odwiedziny[POI 2015]

    题目描述 给定一棵n个点的树,树上每条边的长度都为1,第i个点的权值为a[i]. Byteasar想要走遍这整棵树,他会按照某个1到n的全排列b走n-1次,第i次他会从b[i]点走到b[i+1]点,并 ...

  10. 自己动手写把”锁”---LockSupport介绍

    本篇是<自己动手写把"锁">系列技术铺垫的最后一个知识点.本篇主要讲解LockSupport工具类,它用来实现线程的挂起和唤醒. LockSupport是Java6引入 ...