CodeForces - 298C Parity Game 思維
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 ainto b?
The parity
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
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串
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.題解:
我們可以發現當字串起始1個個數為奇數時,我們只能新增1,為偶數時可以新增任何數量的0,我們又可以刪去左邊的數,當刪去一個1的時候,右邊又可以新增1,這個過程可以說明,串a的1若為偶數num,可以轉化為1數量相同的任意串,若為奇數num,可以轉化1的數量為num或num+1的任何串,所以統計兩個串的1的數量比較即可。
#include<bits/stdc++.h>
using namespace std;
const int N=2*1e6+10;
int n,m,k;
char a[N],b[N];
int main()
{
while(~scanf("%s%s",a+1,b+1))
{
int l1=strlen(a+1);
int l2=strlen(b+1);
int s1=0,s2=0;
for(int i=1;i<=l1;i++)
{
if(a[i]=='1') s1++;
}
for(int j=1;j<=l2;j++)
{
if(b[j]=='1') s2++;
}
if(s1&1) s1++;
// cout<<s1<<" "<<s2<<endl;
if(s1>=s2) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}