UPC-ICPC训练赛 Menagerie(模拟)

题目(http://icpc.upc.edu.cn/problem.php?cid=1713&pid=1)
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.

输入

The input is given from Standard Input in the following format:
N
s

输出

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.

样例输入
6
ooxoox

样例输出
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.
在这里插入图片描述
题目大意:
羊:若它两边的动物不同,它会说x,否则说o;狼与羊相反;
问是否有一种动物排序(环状)使得其说话的序列与输入相同,若有输出,否则输出-1;

///模拟,枚举前两个位置的情况,根据这个情况来不断计算出下一个位置是羊还是狼
///直至最后一个位置
int a1[5]={0,1,-1,1,-1};///动物数组
int a2[5]={0,1,-1,-1,1};///动物数组
int a[100009];
int getnext(int a,int b,char c)///获得下一个位置的动物情况
{
    if(b==1)///为羊
    {
        if(c=='o')return a;
        else return -a;
    }
    else///为狼
    {
        if(c=='o') return -a;
        else return a;
    }
}
int main()
{
    char ani[100005];
    int n;cin>>n;
    cin>>ani+1;///从ani[1]开始输入
    int flag=0;
    for(int i=1;i<5;i++)///枚举第一二个位置动物的情况
    {
        a[1]=a1[i],a[2]=a2[i];
        for(int j=3;j<=n+1;j++)
        {
            a[j]=getnext(a[j-2],a[j-1],ani[j-1]);///获得下一个位置情况
        }
        int c=getnext(a[2],a[1],ani[1]);
        ///判断,由最后一个位置a[n]得出来的第一个位置a[n+1]是否和第一个a[1]的情况相同
        ///由第一个a[1]得出来的最后一个位置c是否和最后一个位置a[n]相同
        if(c==a[n]&&a[n+1]==a[1])
        {
            flag=1;
            break;
        }
    }
    if(flag)
    {
        for(int i=1;i<=n;i++)
        {
            if(a[i]==1)cout<<'S';
            else cout<<'W';
        }
    }
    else cout<<-1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值