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;elseendd=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;elseendd=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;elseendd=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;elseendd=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");elseprintf("W");}printf("\n");}return 0; }?
總結
以上是生活随笔為你收集整理的Menagerie(AtCoder-2234)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sona(NBUT-1457)
- 下一篇: Can you find it(HDU-