Row

题目链接:http://codeforces.com/contest/982/problem/A
A. Row
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You're given a row with n

chairs. We call a seating of people "maximal" if the two following conditions hold:

  1. There are no neighbors adjacent to anyone seated.
  2. 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 n2

).

Input

The first line contains a single integer n

( 1n1000

) — the number of chairs.

The next line contains a string of n

characters, each of them is either zero or one, describing the seating.

Output

Output "Yes" (without quotation marks) if the seating is "maximal". Otherwise print "No".

You are allowed to print letters in whatever case you'd like (uppercase or lowercase).

Examples
Input
Copy
3
101
Output
Copy
Yes
Input
Copy
4
1011
Output
Copy
No
Input
Copy
5
10001
Output
Copy
No
Note

In sample case one the given seating is maximal.

In sample case two the person at chair three has a neighbour to the right.

In sample case three it is possible to seat yet another person into chair three.

题目大意    :就是给一个字符串,然后判断一下1和0是不是相互挨着

解题思路:从第一个开始跑,每一次跑判断这个的前一个和后一个与当前这个是不是有矛盾。

代码如下:

#include
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"<    else
        cout<<"No"<    return 0;

}

需要注意的是:当某一个座位是0的时候,之后满足这个座位的前面和后面都是0时才判定不符合。



你可能感兴趣的:(Row)