Codeforces Round #173 (Div. 2) Problem C

Source: Internet
Author: User

C. XOR and OR


There are two strings, a and B. They are composed of 0 and 1 and are not empty;

Change Rule, 1. It can be changed to 2. Select two characters from the string, two types of operations: xor and or; (^ and |); and ask whether the two strings can be changed;


Through the xor and or rules, we can find that 01 can be changed to 11, 11 can be changed to 01 or 10, but it cannot be changed from 11 to 00;

Therefore, if one of a or B contains only 0 and does not contain 1, the conversion cannot be performed; otherwise, the conversion can be performed;

(Of course, if the length of a and B is different, there is NO doubt NO );

The code I started to write was cumbersome. I simplified it step by step, and my thoughts didn't change;

Code 1:

#define maxn 1000100char a[maxn],b[maxn];using namespace std;int main(int argc, char *argv[]){int a_1,b_1;a_1  = b_1 = 0;gets(a);gets(b);if(strlen(a) != strlen(b)){puts("NO");return 0;}for(int i = 0; i < strlen(a); i++){if(a[i]=='1')a_1++;}for(int i = 0; i < strlen(b); i++){if(b[i] == '1')b_1++;}if((a_1==0&&b_1!=0)||(a_1!=0&&b_1==0)){puts("NO");return 0;}elseputs("YES");return 0;}

Code 2:

//Time 46MSMemory 2000KBint main(int argc, char *argv[]){    gets(a);    gets(b);    if(strlen(a) != strlen(b))    {        puts("NO");        return 0;    }    if((strstr(a,"1")==NULL && strstr(b,"1")!=NULL)||(strstr(a,"1")!=NULL && strstr(b,"1")==NULL))        puts("NO");    else        puts("YES");    return 0;}

Final code:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <iomanip>#define maxn 1000100char a[maxn],b[maxn];using namespace std;int main(int argc, char *argv[]){gets(a);gets(b);if(strlen(a) != strlen(b)){puts("NO");return 0;}if((strstr(a,"1")==NULL) ^ (strstr(b,"1")==NULL))puts("NO");elseputs("YES");return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.