Menagerie(AtCoder-2234)

Problem Description

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 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.

Example

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

题意:n 只动物排成一个环,其中有若干只羊和若干只狼,现在从第一个动物开始询问,问其相邻的两动物是否为同一物种。对于羊来说,若是同一物种则说 o,若不是则说 x;对于狼来说,若是同一物种则说 x,若不是则说 o。根据给出的 n 个 o、x 序列,问是否有一种可分配的合法方案,若有则从 1 号输出到 n 号,其中,羊为 S,狼为 W,若没有则输出 -1

思路:

根据题意可知,如果确定了前两个动物的种类即可确定出 n 个动物的种类,那么总共有 4 种情况:羊羊、羊狼、狼狼、狼羊

因此,从第 2 个开始枚举到 n-1,根据第 i 个动物的询问(str[i])与第 i 个动物的状态(res[i]),即可确定出下一个动物的状态,从而得出 1~n 的动物状态

由于这样枚举而出的状态无法确定是否合法,因此我们可以根据第 1、2 个动物的询问与状态确定出最后一个动物的状态(存进 endd 中),再从第 2 个枚举到 n,根据 n 和 n-1 确定出第一个动物的状态(存进 res[n-1] 中),然后进行判断

如果根据枚举推出的最后一个动物的状态(res[n])与根据第 1、2 个得出的最后一个动物的状态(endd)相同,且枚举推出的第一个动物的状态(res[n+1])与第一个的动物状态(res[1])相同,那么说明该种情况最初的假设成立,直接输出即可

反正,如果以上两个条件任意一个不满足,那么说明该种情况最初的假设不成立,枚举下一种情况,如果 4 种情况都不成立,则说明没有符合条件分配方案,输出 -1

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
char str[N];
int res[N];
void cal(int i){
    if(str[i]=='o'&&res[i]==0){
        res[i+1]=res[i-1];
    }
    else if(str[i]=='o'&&res[i]==1){
        res[i+1]=!res[i-1];
    }
    else if(str[i]=='x'&&res[i]==0){
        res[i+1]=!res[i-1];
    }
    else if(str[i]=='x'&&res[i]==1){
        res[i+1]=res[i-1];
    }
}
int main(){
    int n;
    scanf("%d",&n);
    scanf("%s",str+1);

    int endd;
    bool flag=false;

    res[1]=0;res[2]=0;
    if(str[1]=='o')
        endd=0;
    else
        endd=1;
    for(int i=2;i<=n;i++)
        cal(i);
    if(res[n]==endd&&res[1]==res[n+1])
        flag=true;

    if(!flag){
        res[1]=0;res[2]=1;
        if(str[1]=='o')
            endd=1;
        else
            endd=0;
        for(int i=2;i<=n;i++)
            cal(i);
        if(res[n]==endd&&res[1]==res[n+1])
            flag=true;
    }
    if(!flag){
        res[1]=1;res[2]=0;
        if(str[1]=='o')
            endd=1;
        else
            endd=0;
        for(int i=2;i<=n;i++)
            cal(i);
        if(res[n]==endd&&res[1]==res[n+1])
            flag=true;

    }
    if(!flag){
        res[1]=1;res[2]=1;
        if(str[1]=='o')
            endd=0;
        else
            endd=1;
        for(int i=2;i<=n;i++)
            cal(i);
        if(res[n]==endd&&res[1]==res[n+1])
            flag=true;
    }

    if(!flag)
        printf("-1\n");
    else{
        for(int i=1;i<=n;i++){
            if(res[i]==0)
                printf("S");
            else
                printf("W");
        }
        printf("\n");
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值