zoukankan      html  css  js  c++  java
  • 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;
    }


  • 相关阅读:
    【虎牙直播源】浏览器抓取真实直播源地址(纯前端JS解析源码)
    更加方便获取eid和fp的一种方式-通过HTML文件【京东飞天茅台1499抢购】
    一万字详解 Redis Cluster Gossip 协议
    一种离谱到极致的页面侧边栏效果探究
    人工智能能力提升指导总结
    超炫100套❤vue/react+echarts❤ 大屏可视化数据平台实战项目分享 (附源码)
    大厂Redis高并发场景设计,面试问的都在这!
    Vue 项目性能优化 —实战—面试
    函子的详细解析与发展历程
    B20J_2243_[SDOI2011]染色_树链剖分+线段树
  • 原文地址:https://www.cnblogs.com/youdiankun/p/3633080.html
Copyright © 2011-2022 走看看