zoukankan      html  css  js  c++  java
  • CodeForces 282C(位运算)

    C. XOR and OR
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

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

    题意:给出两个字符串a, b,可以对a字符串中任意相邻的两个字符x,y进行位异或和按位或操作得到p=x^y;q=x|y;
    再用p,q代替原来的x,y(可以交换顺序),问能否得到目标串b;
    思路:
    1:如果a和b的长度不同,则肯定不行;
    2:由位运算法则我们可以知道有:
    0^0=0, 0|0=0;
    1^0=1, 1|0=1;
    0^1=1, 0|1=1;
    1^1=0, 1|1=1;
    据此可以得知:(1,1)可以得到(0,1)或者(1,0);(0,1)或者(1,0)可以得到(1,1);(0,0)只能得到(0,0);
    即有1的串不能完全消掉1(若a串长度为len1,且含有1,那么其可以得到一个含有x个1的串,x>=1&&x<=len1);没有1的串不能得到1;
    所以只有当a,和b同时含有1或者不含1时可行;

    代码:
    
    
     1 #include <bits/stdc++.h>
     2 #define MAXN 100000+10
     3 #define ll long long
     4 using namespace std;
     5 
     6 int main(void)
     7 {
     8     std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
     9     string a, b;
    10     cin >> a >> b;
    11     int len1=a.size();
    12     int len2=b.size();
    13     if(len1!=len2)
    14     {
    15         cout << "NO" << endl;
    16         return 0;
    17     }
    18     if(len1==1&&a[0]=='1')
    19     {
    20         if(b[0]=='1') cout << "YES" << endl;
    21         else cout << "NO" << endl;
    22         return 0;
    23     }
    24     int ans1=0, ans2=0;
    25     for(int i=0; i<len1; i++)
    26     {
    27         if(a[i]=='1')  ans1++;
    28         if(b[i]=='1')  ans2++;
    29     }
    30     if(!ans1&&!ans2||ans1&&ans2)
    31     cout << "YES" << endl;
    32     else cout << "NO" << endl;
    33     return 0;
    34 }
    
    
    
     

    艰难困苦,玉汝于成 —————— geloutingyu
  • 相关阅读:
    加密
    读取excel
    poj 1852 Ants
    关于运行时间
    poj 1001 Exponentiation
    Poj 3669 Meteor Shower
    一道简单题目的优化过程——抽签问题
    高精度四则运算
    Usaco_Contest_2013_Open_Bovine Problem 1. Bovine Ballet
    h5 音频 视频全屏设置
  • 原文地址:https://www.cnblogs.com/geloutingyu/p/5877030.html
Copyright © 2011-2022 走看看