• XOR and OR


    XOR and OR

    Description

    The Bitlandians are quite weird people. They do everything differently. They have a different alphabet so they have a different definition for a string.

    A Bitlandish string is a string made only of characters "0" and "1".

    BitHaval (the mayor of Bitland) loves to play with Bitlandish strings. He takes some Bitlandish string a, and applies several (possibly zero) operations to it. In one operation the mayor may take any two adjacent characters of a string, define one of them as x and the other one as y. Then he calculates two values p and q: p = x xor y, q = x or y. Then he replaces one of the two taken characters by p and the other one by q.

    The xor operation means the bitwise excluding OR operation. The or operation is the bitwise OR operation.

    So for example one operation can transform string 11 to string 10 or to string 01. String 1 cannot be transformed into any other string.

    You've got two Bitlandish strings a and b. Your task is to check if it is possible for BitHaval to transform string a to string b in several (possibly zero) described operations.

    Input

    The first line contains Bitlandish string a, the second line contains Bitlandish string b. The strings can have different lengths.

    It is guaranteed that the given strings only consist of characters "0" and "1". The strings are not empty, their length doesn't exceed 106.

    Output

    Print "YES" if a can be transformed into b, otherwise print "NO". Please do not print the quotes.

    Sample Input

    Input
    11
    10
    Output
    YES
    Input
    1
    01
    Output
    NO
    Input
    000
    101
    Output
    NO

    (异或:相同为0,不同为1)

    其实想明白了很简单、很有意思的一个题。如果两个字符串的长度不相等,则怎么变换也是NO。

    让我们来设想一种简单的情况,当两个字符串长度都为2时:

    1、 00->00(始终)
    2、 01->11再做操作11->10或者11->01
    3、 10->11再做操作11->10或者11->01
    4、 11->10或者11->01

    由2、3、4可知(别忘了可做多步操作),都无法到00。

    所以两边有1的都是YES,两边全是0的也是YES。

    #include<cstdio>
    #include<cstring>
    #define maxn 1000001
    
    char a[maxn], b[maxn];
    
    int main()
    {
        int len1,len2;
        int i,flag1,flag2;
        while(gets(a) != 0)
        {
            flag1=flag2=0;
            gets(b);
            len1=strlen(a);
            len2=strlen(b);
            if(len1!=len2)
            {
                printf("NO
    ");
                continue;
            }
            for(i=0;i<len1;i++)
            {
                if(a[i]=='1')
                {
                    flag1=1;
                    break;
                }
            }
            for(i=0;i<len2;i++)
            {
                if(b[i]=='1')
                {
                    flag2=1;
                    break;
                }
            }
            if(!flag1&&!flag2)
            {
                printf("YES
    ");
            }
            else if(flag1 && flag2)
            {
                printf("YES
    ");
            }
            else
                printf("NO
    ");
        }
        return 0;
    }


  • 相关阅读:
    Mybatis和Hibernate
    SpringMVC运行原理
    HTML中class属性里有多个属性值
    json对象(增加数据)
    Ajax请求
    <url-pattern>里的/和/*
    mysql忘记密码
    Eclipse无法正常连接mysql8.0.20
    a+1和&a+1
    可重入函数与不可重入函数
  • 原文地址:https://www.cnblogs.com/youdiankun/p/3633080.html
Copyright © 2020-2023  润新知