CF-298C(模拟+找规律)

C. Parity Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations:

  • Write parity(a) to the end of a. For example, .
  • Remove the first character of a. For example, . You cannot perform this operation if a is empty.

You can use as many operations as you want. The problem is, is it possible to turn a into b?

The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.

Input

The first line contains the string a and the second line contains the string b (1 ≤ |a|, |b| ≤ 1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.

Output

Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.

Examples
input
01011
0110
output
YES
input
0011
1110
output
NO
Note

In the first sample, the steps are as follows: 01011 → 1011 → 011 → 0110

题意:给两个串。问能不能由a串经过变换转化成b串。变换规则:删去最左边一位。在右边加上一位,所加位的值取决于串中1的总数。如果串中有偶数个1,那么久在右边加0,如果串中有奇数个1,那么就在右边加1。

思路:模拟。按照规则,考虑a串中最多能有多少个1。

如果原始a串中的1总数为奇数pa,那么根据规则还能再串尾加一个1,即pa+1。a串中最多有pa+1个1。

如果原始a串中的1总数为偶数pa,那么不能再加1了。a串中最多有pa个1。

下面考虑如何变成b串。首先我们先把原始串中的1变到最多(如果pa是奇数,在结尾加个1)。之后我们在a串后面构造b串。如果对应b串的位置是0,那么直接在a串后边加0就好(因为pa是偶数)。如果对应b串的位置是1,那么删掉a串前的字符,直到删掉了一个1,让pa变为奇数为止。因为pa是奇数,所以在a串后加一个1。相当于将前面的一个1移动到了最后。pa又变成了偶数。然后在继续下去。。。如果是0,直接加,如果是1,就删了再加。

所以我们只需考虑。a中1的总数是否大于b中1的总数。如果大于,则按照上面的构造方法能够构造出b串,否则就构造不出来。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 
 5 using namespace std;
 6 
 7 char a[1010];
 8 char b[1010];
 9 
10 int main()
11 {
12     while(~scanf("%s %s", a, b)){
13         int pa = 0, pb = 0;
14         for (int i = 0; i < strlen(a); i++){
15             if(a[i] == '1')
16                 pa++;
17         }
18         for (int i = 0; i < strlen(b); i++){
19             if(b[i] == '1')
20                 pb++;
21         }
22         if(pa % 2 == 1)//a中最多的1数
23             pa++;
24         if(pa >= pb)
25             printf("YES\n");
26         else{
27             printf("NO\n");
28         }
29     }
30     return 0;
31 }

 

posted @ 2016-02-26 23:36  喷水小火龙  阅读(176)  评论(0)    收藏  举报