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.求最少需要几 ...
随机推荐
- SSH连接工具:SecureCRT设置,另一个SSH连接工具:Xshell。在Windows和Linux之间互传文件可用WinSCP
一般Linux发行版不允许root远程登录,CentOS允许. 调整字体大小:
- Layui常见问题
为什么表单不显示?当你使用表单时,Layui会对select.checkbox.radio等原始元素隐藏,从而进行美化修饰处理.但这需要依赖于form组件,所以你必须加载 form,并且执行一个实例. ...
- Vue.js(一)了解Vue
什么是Vue? 1.Vue.js是一个构建数据驱动的web界面的库.类似于Angularjs,在技术上,他重点集中在MVVM模式的View层,非常容易学习,非常容易和其他的库或已有的项目整合. 2.V ...
- bzoj 3712: [PA2014]Fiolki
Description 化学家吉丽想要配置一种神奇的药水来拯救世界.吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[i]克的第i种物质.吉丽需要执行一定的步骤来配置药 ...
- localStorage用法总结
这些知识是参考下面的朋友的.谢谢分享. http://www.jianshu.com/p/39ba41ead42e http://www.cnblogs.com/st-leslie/p/5617130 ...
- Mysql的硬件优化和配置优化
mysql数据库的优化,算是一个老生常谈的问题了,网上也有很多关于各方面性能优化的例子,今天我们要谈的是MySQL硬件优化和系统参数的优化-即优化my.cnf文件 MySQL的优化我分为两个部分,一是 ...
- CentOS下LAMP环境安装配置
本来几下yum都能装好的,yum却出问题了,报错:AttributeError: 'YumBaseCli' object has no attribute '_not_found_i',可能是某个文件 ...
- Python学习_10__python2到python3
同样作为动态语言,python的面相对像和ruby有很多类似的地方,这里还是推荐<Ruby元编程>一书来参考学习python的面向对象.然而python并不是纯面向对象设计,所以很多rub ...
- 关于linux命令ssh的总结
因为项目计算量比较大,需要将任务分布到多台电脑上面运行,因为对于分布式概念不熟,就想到了linux最简单的ssh协议,远程控制其他电脑,然后写shell脚本统一在所有电脑上运行程序.(我的操作系统为U ...
- git for windows上传项目到github
软件:git for windows 账户:github账户 1.第一步创建自己的github账号,并创建自己的project,创建完毕之后url如下 https://github.com/ft110 ...