You're given a row with n
chairs. We call a seating of people "maximal" if the two following conditions hold:
- There are no neighbors adjacent to anyone seated.
- It's impossible to seat one more person without violating the first rule.
The seating is given as a string consisting of zeros and ones (0
means that the corresponding seat is empty, 1— occupied). The goal is to determine whether this seating is "maximal".
Note that the first and last seats are not adjacent (if n≠2
).
The first line contains a single integer n
) — the number of chairs.
The next line contains a string of n
解题思路:从第一个开始跑,每一次跑判断这个的前一个和后一个与当前这个是不是有矛盾。
代码如下:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
char s[1004];
cin>>s+1;
int flag=1;
for(int i=1; i<=n; i++)
{
if(s[i]=='1')
{
if((s[i-1]=='1'&&i!=1)||(s[i+1]=='1'&&i!=n))
flag=0;
}
else
{
if((s[i-1]=='0'||i==1)&&(s[i+1]=='0'||i=n))
flag=0;
}
}
if(flag)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return 0;
}
需要注意的是:当某一个座位是0的时候,之后满足这个座位的前面和后面都是0时才判定不符合。