【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?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 0110Output
YESInput
0011 1110Output
NONote
In the first sample, the steps are as follows:?01011?→?1011?→?011?→?0110
題目大意:
? 告訴你兩種操作,問你能否將A串變成B串。操作1:刪除第一個字符。操作2:在字符串后面添parity(a),這個函數(shù)的值為0或1,分別在 串中有偶數(shù)個1,奇數(shù)個1? 時取到。
解題報告:
? 猜結論的思維題。。。模擬了一下發(fā)現(xiàn)真的可以,,。也就是看字符串中最多能造出多少個1來。,因為如果1 的數(shù)量大于等于b串中1的數(shù)量,,就一定能構造出來、、就是忘了如果1的個數(shù)為奇數(shù)的時候,,我們可以直接在后面添加一個1變成多一個1,
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e6 + 5; int n,k; char s[MAX],t[MAX]; int main() {scanf("%s",s+1);scanf("%s",t+1);int cnt1=0,cnt2=0;for(int i = 1; i<=strlen(s+1); i++) {if(s[i] == '1') cnt1++;}for(int i = 1; i<=strlen(t+1); i++) {if(t[i] == '1') cnt2++;}if(cnt1&1) cnt1++;if(cnt1 >= cnt2) puts("YES");else puts("NO");return 0 ;}?
總結
以上是生活随笔為你收集整理的【CodeForces - 298C】Parity Game (思维,有坑)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【POJ - 2631 】Roads i
- 下一篇: 【HDU - 2012】素数判定(水题,